Skip to content

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

Closed
@catb0t

Description

@catb0t

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions