-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improved error messaging for index signature parameters #20726
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
Changes from 4 commits
0c089d8
a0d827c
92bffe4
8755d4d
c92c7a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -939,6 +939,14 @@ | |
"category": "Error", | ||
"code": 1335 | ||
}, | ||
"An index signature parameter type cannot be a type alias. Use '[index: {0}]' instead.": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"category": "Error", | ||
"code": 1336 | ||
}, | ||
"An index signature parameter type cannot be a union type. Use '[K in {0}]' instead.": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this won't work if there are any other members in the object type. Maybe we should just write "Consider using a mapped object type instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry, totally misunderstood that. |
||
"category": "Error", | ||
"code": 1337 | ||
}, | ||
|
||
"Duplicate identifier '{0}'.": { | ||
"category": "Error", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
tests/cases/compiler/indexerConstraints2.ts(9,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'. | ||
tests/cases/compiler/indexerConstraints2.ts(17,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'. | ||
tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'. | ||
tests/cases/compiler/indexerConstraints2.ts(34,6): error TS1336: An index signature parameter type cannot be a type alias. Use '[index: number]' instead. | ||
tests/cases/compiler/indexerConstraints2.ts(40,6): error TS1336: An index signature parameter type cannot be a type alias. Use '[index: string]' instead. | ||
tests/cases/compiler/indexerConstraints2.ts(46,6): error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
tests/cases/compiler/indexerConstraints2.ts(52,6): error TS1337: An index signature parameter type cannot be a union type. Use '[K in IndexableUnion]' instead. | ||
tests/cases/compiler/indexerConstraints2.ts(58,6): error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
tests/cases/compiler/indexerConstraints2.ts(64,6): error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
tests/cases/compiler/indexerConstraints2.ts(70,6): error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
|
||
|
||
==== tests/cases/compiler/indexerConstraints2.ts (3 errors) ==== | ||
==== tests/cases/compiler/indexerConstraints2.ts (10 errors) ==== | ||
class A { a: number; } | ||
class B extends A { b: number; } | ||
|
||
|
@@ -37,4 +44,61 @@ tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index t | |
~~~~~~~~~~~~~~~ | ||
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'. | ||
[s: string]: B; | ||
} | ||
|
||
|
||
type AliasedNumber = number; | ||
|
||
interface L { | ||
[n: AliasedNumber]: A; | ||
~ | ||
!!! error TS1336: An index signature parameter type cannot be a type alias. Use '[index: number]' instead. | ||
} | ||
|
||
type AliasedString = string; | ||
|
||
interface M { | ||
[s: AliasedString]: A; | ||
~ | ||
!!! error TS1336: An index signature parameter type cannot be a type alias. Use '[index: string]' instead. | ||
} | ||
|
||
type AliasedBoolean = boolean; | ||
|
||
interface N { | ||
[b: AliasedBoolean]: A; | ||
~ | ||
!!! error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
} | ||
|
||
type IndexableUnion = "foo" | "bar"; | ||
|
||
interface O { | ||
[u: IndexableUnion]: A; | ||
~ | ||
!!! error TS1337: An index signature parameter type cannot be a union type. Use '[K in IndexableUnion]' instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This version of the error message should only appear when the parent is a type alias There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given this suggestion #20726 (comment), is it still applicable? |
||
} | ||
|
||
type NonIndexableUnion = boolean | {}; | ||
|
||
interface P { | ||
[u: NonIndexableUnion]: A; | ||
~ | ||
!!! error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
} | ||
|
||
type NonIndexableUnion2 = string | number; | ||
|
||
interface Q { | ||
[u: NonIndexableUnion2]: A; | ||
~ | ||
!!! error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
} | ||
|
||
type NonIndexableUnion3 = "foo" | 42; | ||
|
||
interface R { | ||
[u: NonIndexableUnion3]: A; | ||
~ | ||
!!! error TS1023: An index signature parameter type must be 'string' or 'number'. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allTypesAssignableToKind