Skip to content

fix #33427 #33486

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

Merged
merged 4 commits into from Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32797,12 +32797,12 @@ namespace ts {
typeToString(node.type ? getTypeFromTypeNode(node.type) : anyType));
}

if (type.flags & TypeFlags.Union && allTypesAssignableToKind(type, TypeFlags.StringLiteral, /*strict*/ true)) {
if (type.flags & TypeFlags.Union && allTypesAssignableToKind(type, TypeFlags.StringOrNumberLiteral, /*strict*/ true)) {
return grammarErrorOnNode(parameter.name,
Diagnostics.An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead);
}

return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_string_or_number);
return grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_either_string_or_number);
}
if (!node.type) {
return grammarErrorOnNode(node, Diagnostics.An_index_signature_must_have_a_type_annotation);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"category": "Error",
"code": 1022
},
"An index signature parameter type must be 'string' or 'number'.": {
"An index signature parameter type must be either 'string' or 'number'.": {
"category": "Error",
"code": 1023
},
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arraySigChecking.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/arraySigChecking.ts(11,17): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/arraySigChecking.ts(11,17): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/arraySigChecking.ts(18,27): error TS2322: Type 'void' is not assignable to type 'string'.
tests/cases/compiler/arraySigChecking.ts(22,13): error TS2322: Type 'number' is not assignable to type 'number[]'.
tests/cases/compiler/arraySigChecking.ts(22,16): error TS2322: Type 'number' is not assignable to type 'number[]'.
Expand All @@ -17,7 +17,7 @@ tests/cases/compiler/arraySigChecking.ts(22,16): error TS2322: Type 'number' is

var foo: { [index: any]; }; // expect an error here
~~~~~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}

interface myInt {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/bigintIndex.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/a.ts(2,6): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/a.ts(2,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/a.ts(8,11): error TS2538: Type '1n' cannot be used as an index type.
tests/cases/compiler/a.ts(14,1): error TS2322: Type '123n' is not assignable to type 'string | number | symbol'.
tests/cases/compiler/a.ts(19,12): error TS2538: Type 'bigint' cannot be used as an index type.
Expand All @@ -13,7 +13,7 @@ tests/cases/compiler/b.ts(4,12): error TS2464: A computed property name must be
interface BigIntIndex<E> {
[index: bigint]: E; // should error
~~~~~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}

const arr: number[] = [1, 2, 3];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,6): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,13): error TS2304: Cannot find name 'TypeNotFound'.
tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,13): error TS4092: Parameter 'index' of index signature from exported interface has or is using private name 'TypeNotFound'.

Expand All @@ -7,7 +7,7 @@ tests/cases/compiler/declarationEmitIndexTypeNotFound.ts(2,13): error TS4092: Pa
export interface Test {
[index: TypeNotFound]: any;
~~~~~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'TypeNotFound'.
~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts(1,33): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts(1,33): error TS1023: An index signature parameter type must be either 'string' or 'number'.


==== tests/cases/compiler/genericIndexTypeHasSensibleErrorMessage.ts (1 errors) ====
type Wat<T extends string> = { [x: T]: string };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(10,21): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(11,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(11,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,19): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,19): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(12,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.d.ts(14,23): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
Expand Down Expand Up @@ -36,7 +36,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
declare var d: { [x: C]: C };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(10,13): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(11,14): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(11,18): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,11): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,11): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,14): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(12,18): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument.ts(14,13): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
Expand Down Expand Up @@ -46,7 +46,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
var d: { [x: C]: C };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(10,13): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(11,14): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(11,18): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,11): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,11): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,14): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(12,18): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument2.ts(14,13): error TS2314: Generic type 'I<T>' requires 1 type argument(s).
Expand Down Expand Up @@ -46,7 +46,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
!!! error TS2314: Generic type 'I<T>' requires 1 type argument(s).
var d: { [x: I]: I };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
~
!!! error TS2314: Generic type 'I<T>' requires 1 type argument(s).
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(10,21): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(11,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(11,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,19): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,19): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,22): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(12,26): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenceWithoutTypeArgument3.ts(14,23): error TS2314: Generic type 'C<T>' requires 1 type argument(s).
Expand Down Expand Up @@ -36,7 +36,7 @@ tests/cases/conformance/types/specifyingTypes/typeReferences/genericTypeReferenc
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
declare var d: { [x: C]: C };
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
~
!!! error TS2314: Generic type 'C<T>' requires 1 type argument(s).
~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/indexTypeCheck.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type '
tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'.
tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
tests/cases/compiler/indexTypeCheck.ts(32,3): error TS1096: An index signature must have exactly one parameter.
tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/indexTypeCheck.ts(51,8): error TS2538: Type 'Blue' cannot be used as an index type.


Expand Down Expand Up @@ -58,7 +58,7 @@ tests/cases/compiler/indexTypeCheck.ts(51,8): error TS2538: Type 'Blue' cannot b
interface Magenta {
[p:Purple]; // error
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}

var yellow: Yellow;
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/indexerConstraints2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ tests/cases/compiler/indexerConstraints2.ts(17,5): error TS2413: Numeric index t
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. Consider writing '[n: number]: A' instead.
tests/cases/compiler/indexerConstraints2.ts(40,6): error TS1336: An index signature parameter type cannot be a type alias. Consider writing '[s: string]: A' 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(46,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/indexerConstraints2.ts(52,6): error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type 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(58,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/indexerConstraints2.ts(64,6): error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
tests/cases/compiler/indexerConstraints2.ts(70,6): error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
tests/cases/compiler/indexerConstraints2.ts(74,6): error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
tests/cases/compiler/indexerConstraints2.ts(79,6): error TS1336: An index signature parameter type cannot be a type alias. Consider writing '[key: string]: any' instead.

Expand Down Expand Up @@ -70,7 +70,7 @@ tests/cases/compiler/indexerConstraints2.ts(79,6): error TS1336: An index signat
interface N {
[b: AliasedBoolean]: A;
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}

type IndexableUnion = "foo" | "bar";
Expand All @@ -86,23 +86,23 @@ tests/cases/compiler/indexerConstraints2.ts(79,6): error TS1336: An index signat
interface P {
[u: NonIndexableUnion]: A;
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}

type NonIndexableUnion2 = string | number;

interface Q {
[u: NonIndexableUnion2]: A;
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
}

type NonIndexableUnion3 = "foo" | 42;

interface R {
[u: NonIndexableUnion3]: A;
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
}

interface S {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/parserES5SymbolIndexer1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer1.ts(2,6): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer1.ts(2,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.


==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer1.ts (1 errors) ====
interface I {
[s: symbol]: string;
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/parserES5SymbolIndexer2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer2.ts(2,6): error TS1023: An index signature parameter type must be 'string' or 'number'.
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer2.ts(2,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.


==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolIndexer2.ts (1 errors) ====
class C {
[s: symbol]: string;
~
!!! error TS1023: An index signature parameter type must be 'string' or 'number'.
!!! error TS1023: An index signature parameter type must be either 'string' or 'number'.
}
Loading