Skip to content
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

Union index signature parameter type has unclear / incorrect error #33427

Closed
catb0t opened this issue Sep 14, 2019 · 3 comments · Fixed by #33486
Closed

Union index signature parameter type has unclear / incorrect error #33427

catb0t opened this issue Sep 14, 2019 · 3 comments · Fixed by #33486
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@catb0t
Copy link

catb0t commented Sep 14, 2019

TypeScript Version: 3.7.0-dev.20190913

Search Terms:

  • index signature parameter type
  • index signature union

Code

interface A {
  [id : string | number] : boolean;
};

Expected behavior:

Either the following error message is displayed, or the error message makes it clear that only one of string xor number is permissible as an index signature parameter type.

"An index signature parameter type cannot be a union type. Consider using a mapped object type instead.": {
"category": "Error",
"code": 1337
},

Actual behavior:

error TS1023: An index signature parameter type must be 'string' or 'number'.

To me, the expression string | number is equivalent to the phrase "'string' or 'number'", but the phrasing is not precise.

Either the wrong error is being thrown, or this TS1023 should be clarified to say "...must be one of 'string' or 'number'." or "...must be either 'string' or 'number'."

"An index signature parameter type must be 'string' or 'number'.": {
"category": "Error",
"code": 1023
},

Playground Link:
(error not shown in Playground)

Related Issues:

#2611
#2049
#8711
#18759

I will fix this in a PR if you tell me whether TS1023 is the right error with a fixed message, or if TS1337 should be thrown instead of TS1023.

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging labels Sep 14, 2019
@DanielRosenwasser
Copy link
Member

I think you are correct that we should give that error message. Feel free to send a PR!

@DanielRosenwasser DanielRosenwasser added Good First Issue Well scoped, documented and has the green light Help Wanted You can do this labels Sep 14, 2019
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.7.0 milestone Sep 14, 2019
@fatcerberus
Copy link

An index signature parameter type cannot be a union type. Consider using a mapped object type instead.

I personally really like this one, as it's been my experience that this is generally what people are trying to do when they write such a signature. Alternatively, it could suggest using Record.

@ghost ghost mentioned this issue Sep 18, 2019
DanielRosenwasser added a commit that referenced this issue Oct 10, 2019
@DanielRosenwasser
Copy link
Member

The current change is a good improvement and we can iterate based on feedback. Thanks @TroyTae!

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants