Skip to content

Commit 8543d30

Browse files
authored
Add union check to improved error message (#23027)
1 parent a53f9e8 commit 8543d30

6 files changed

+27
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26626,7 +26626,7 @@ namespace ts {
2662626626
typeToString(getTypeFromTypeNode(node.type)));
2662726627
}
2662826628

26629-
if (allTypesAssignableToKind(type, TypeFlags.StringLiteral, /*strict*/ true)) {
26629+
if (type.flags & TypeFlags.Union && allTypesAssignableToKind(type, TypeFlags.StringLiteral, /*strict*/ true)) {
2663026630
return grammarErrorOnNode(parameter.name,
2663126631
Diagnostics.An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead);
2663226632
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts(1,33): error TS1023: An index signature parameter type must be 'string' or 'number'.
2+
3+
4+
==== tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts (1 errors) ====
5+
type Wat<T extends string> = { [x: T]: string };
6+
~
7+
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//// [genericIndexTypeHasSensibleErrorMessage.ts]
2+
type Wat<T extends string> = { [x: T]: string };
3+
4+
//// [genericIndexTypeHasSensibleErrorMessage.js]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts ===
2+
type Wat<T extends string> = { [x: T]: string };
3+
>Wat : Symbol(Wat, Decl(genericIndexTypeHasSensibleErrorMessage.ts, 0, 0))
4+
>T : Symbol(T, Decl(genericIndexTypeHasSensibleErrorMessage.ts, 0, 9))
5+
>x : Symbol(x, Decl(genericIndexTypeHasSensibleErrorMessage.ts, 0, 32))
6+
>T : Symbol(T, Decl(genericIndexTypeHasSensibleErrorMessage.ts, 0, 9))
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts ===
2+
type Wat<T extends string> = { [x: T]: string };
3+
>Wat : Wat<T>
4+
>T : T
5+
>x : T
6+
>T : T
7+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Wat<T extends string> = { [x: T]: string };

0 commit comments

Comments
 (0)