Skip to content

Relax requirements on index signatures to 'any' when a type also contains a string index signature to 'any' #43065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2021

Conversation

DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Mar 3, 2021

Fixes #43064


When checking compatibility between two types, TypeScript should not require a corresponding source index signature if the target side index signature maps to any and the target side has a string index signature that maps to any.

So an index signature like in

{ [x: number]: any }

is still required of a source type, but neither index signature in

{ [x: number]: any, [x: string]: any; }

should be required; however, the number index signature in

{ [x: number]: number, [x: string]: any; }

should always be required because the number index signature doesn't map to any.


TL;DR:

Given type SomeObject = { hello: string, world: number }:

  • SomeObject -> { [x: string]: any }
  • SomeObject -> { [x: string]: any, [x: number]: any }
  • SomeObject -> { [x: number]: any }
  • SomeObject -> { [x: string]: any, [x: number]: string }

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Mar 3, 2021
@DanielRosenwasser
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 3, 2021

Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at bb8f30c. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

Hey @DanielRosenwasser, something went wrong when looking for the build artifact. (You can check the log here).

@DanielRosenwasser
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 3, 2021

Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at bb8f30c. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

Hey @DanielRosenwasser, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/97536/artifacts?artifactName=tgz&fileId=2157C5B3D6404A7C57C808E06F23B23DF3A70958FF7A37FE60209F4EA88D2ED102&fileName=/typescript-4.3.0-insiders.20210303.tgz"
    }
}

and then running npm install.

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Mar 3, 2021

One thing I just thought of - when #26797 goes in, I think it would be best not to relax the requirements to other index signatures since this is just meant to unbreak code.

Comment on lines +97 to +101
bothToAny = someObj;
~~~~~~~~~
!!! error TS2322: Type 'Obj' is not assignable to type 'StringTo<any> & NumberTo<any>'.
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'.
!!! error TS2322: Index signature is missing in type 'Obj'.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be questionable. This used to work in 4.2, but this PR doesn't un-break this code.


bothToAny = sToAny;
bothToAny = nToAny;
bothToAny = someObj;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the example that's been un-broken.

@DanielRosenwasser DanielRosenwasser merged commit 9862b03 into master Mar 5, 2021
@DanielRosenwasser DanielRosenwasser deleted the alwaysBeHappyWithStringIndexSignatures branch March 5, 2021 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relax 'any' index signature requirements when a type contains a string index signature to 'any'
2 participants