mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Better error messages for aliased index types
This commit is contained in:
1 parent
d89d68f426
commit
0c089d8d66
7 files changed
+151
-1
No files matched your search
@@ -25775,6 +25775,12 @@ namespace ts {
|
||||
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_must_have_a_type_annotation);
|
||||
}
|
||||
if (parameter.type.kind !== SyntaxKind.StringKeyword && parameter.type.kind !== SyntaxKind.NumberKeyword) {
|
||||
const type = getTypeFromTypeNode(parameter.type);
|
||||
|
||||
if (type.flags & TypeFlags.String || type.flags & TypeFlags.Number) {
|
||||
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_cannot_be_a_type_alias_Use_index_Colon_0_instead, typeToString(type));
|
||||
}
|
||||
|
||||
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_string_or_number);
|
||||
}
|
||||
if (!node.type) {
|
||||
|
||||
@@ -939,6 +939,10 @@
|
||||
"category": "Error",
|
||||
"code": 1335
|
||||
},
|
||||
"An index signature parameter type cannot be a type alias. Use '[index: {0}]' instead.": {
|
||||
"category": "Error",
|
||||
"code": 1336
|
||||
},
|
||||
|
||||
"Duplicate identifier '{0}'.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
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 (3 errors) ====
|
||||
==== tests/cases/compiler/indexerConstraints2.ts (6 errors) ====
|
||||
class A { a: number; }
|
||||
class B extends A { b: number; }
|
||||
|
||||
@@ -37,4 +40,29 @@ 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'.
|
||||
}
|
||||
@@ -26,6 +26,25 @@ class J {
|
||||
class K extends J {
|
||||
[n: number]: A;
|
||||
[s: string]: B;
|
||||
}
|
||||
|
||||
|
||||
type AliasedNumber = number;
|
||||
|
||||
interface L {
|
||||
[n: AliasedNumber]: A;
|
||||
}
|
||||
|
||||
type AliasedString = string;
|
||||
|
||||
interface M {
|
||||
[s: AliasedString]: A;
|
||||
}
|
||||
|
||||
type AliasedBoolean = boolean;
|
||||
|
||||
interface N {
|
||||
[b: AliasedBoolean]: A;
|
||||
}
|
||||
|
||||
//// [indexerConstraints2.js]
|
||||
|
||||
@@ -62,3 +62,40 @@ class K extends J {
|
||||
>s : Symbol(s, Decl(indexerConstraints2.ts, 26, 5))
|
||||
>B : Symbol(B, Decl(indexerConstraints2.ts, 0, 22))
|
||||
}
|
||||
|
||||
|
||||
type AliasedNumber = number;
|
||||
>AliasedNumber : Symbol(AliasedNumber, Decl(indexerConstraints2.ts, 27, 1))
|
||||
|
||||
interface L {
|
||||
>L : Symbol(L, Decl(indexerConstraints2.ts, 30, 28))
|
||||
|
||||
[n: AliasedNumber]: A;
|
||||
>n : Symbol(n, Decl(indexerConstraints2.ts, 33, 5))
|
||||
>AliasedNumber : Symbol(AliasedNumber, Decl(indexerConstraints2.ts, 27, 1))
|
||||
>A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0))
|
||||
}
|
||||
|
||||
type AliasedString = string;
|
||||
>AliasedString : Symbol(AliasedString, Decl(indexerConstraints2.ts, 34, 1))
|
||||
|
||||
interface M {
|
||||
>M : Symbol(M, Decl(indexerConstraints2.ts, 36, 28))
|
||||
|
||||
[s: AliasedString]: A;
|
||||
>s : Symbol(s, Decl(indexerConstraints2.ts, 39, 5))
|
||||
>AliasedString : Symbol(AliasedString, Decl(indexerConstraints2.ts, 34, 1))
|
||||
>A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0))
|
||||
}
|
||||
|
||||
type AliasedBoolean = boolean;
|
||||
>AliasedBoolean : Symbol(AliasedBoolean, Decl(indexerConstraints2.ts, 40, 1))
|
||||
|
||||
interface N {
|
||||
>N : Symbol(N, Decl(indexerConstraints2.ts, 42, 30))
|
||||
|
||||
[b: AliasedBoolean]: A;
|
||||
>b : Symbol(b, Decl(indexerConstraints2.ts, 45, 5))
|
||||
>AliasedBoolean : Symbol(AliasedBoolean, Decl(indexerConstraints2.ts, 40, 1))
|
||||
>A : Symbol(A, Decl(indexerConstraints2.ts, 0, 0))
|
||||
}
|
||||
@@ -62,3 +62,40 @@ class K extends J {
|
||||
>s : string
|
||||
>B : B
|
||||
}
|
||||
|
||||
|
||||
type AliasedNumber = number;
|
||||
>AliasedNumber : number
|
||||
|
||||
interface L {
|
||||
>L : L
|
||||
|
||||
[n: AliasedNumber]: A;
|
||||
>n : number
|
||||
>AliasedNumber : number
|
||||
>A : A
|
||||
}
|
||||
|
||||
type AliasedString = string;
|
||||
>AliasedString : string
|
||||
|
||||
interface M {
|
||||
>M : M
|
||||
|
||||
[s: AliasedString]: A;
|
||||
>s : string
|
||||
>AliasedString : string
|
||||
>A : A
|
||||
}
|
||||
|
||||
type AliasedBoolean = boolean;
|
||||
>AliasedBoolean : boolean
|
||||
|
||||
interface N {
|
||||
>N : N
|
||||
|
||||
[b: AliasedBoolean]: A;
|
||||
>b : boolean
|
||||
>AliasedBoolean : boolean
|
||||
>A : A
|
||||
}
|
||||
@@ -25,4 +25,23 @@ class J {
|
||||
class K extends J {
|
||||
[n: number]: A;
|
||||
[s: string]: B;
|
||||
}
|
||||
|
||||
|
||||
type AliasedNumber = number;
|
||||
|
||||
interface L {
|
||||
[n: AliasedNumber]: A;
|
||||
}
|
||||
|
||||
type AliasedString = string;
|
||||
|
||||
interface M {
|
||||
[s: AliasedString]: A;
|
||||
}
|
||||
|
||||
type AliasedBoolean = boolean;
|
||||
|
||||
interface N {
|
||||
[b: AliasedBoolean]: A;
|
||||
}
|
||||
Reference in new issue
Block a user