diff --git a/packages/typescript/src/ast/ast.generated.ts b/packages/typescript/src/ast/ast.generated.ts index f4d3a2ca47f83..9c7b5d897b6a9 100644 --- a/packages/typescript/src/ast/ast.generated.ts +++ b/packages/typescript/src/ast/ast.generated.ts @@ -754,7 +754,6 @@ export interface SetAccessorDeclaration extends NodeBase, DeclarationBase, Named } export interface IndexSignatureDeclaration extends NodeBase, DeclarationBase, ModifiersBase, FunctionLikeBase, TypeElementBase, ClassElementBase { readonly kind: SyntaxKind.IndexSignature; - readonly type: TypeNode; } export interface MethodSignatureDeclaration extends NodeBase, DeclarationBase, NamedMemberBase, FunctionLikeBase, TypeElementBase { readonly kind: SyntaxKind.MethodSignature; diff --git a/packages/typescript/src/ast/factory.generated.ts b/packages/typescript/src/ast/factory.generated.ts index 8bc727d7178a1..aeee534813a87 100644 --- a/packages/typescript/src/ast/factory.generated.ts +++ b/packages/typescript/src/ast/factory.generated.ts @@ -4357,7 +4357,7 @@ export function createSetAccessorDeclaration(modifiers: readonly ModifierLike[] }) as unknown as SetAccessorDeclaration; } -export function createIndexSignatureDeclaration(modifiers: readonly ModifierLike[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode): IndexSignatureDeclaration { +export function createIndexSignatureDeclaration(modifiers: readonly ModifierLike[] | undefined, parameters: readonly ParameterDeclaration[], type?: TypeNode): IndexSignatureDeclaration { return new NodeObject(SyntaxKind.IndexSignature, { modifiers: modifiers ? createNodeArray(modifiers) : undefined, parameters: createNodeArray(parameters), @@ -5591,7 +5591,7 @@ export function updateSetAccessorDeclaration(node: SetAccessorDeclaration, modif return node.modifiers !== modifiers || node.name !== name || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body ? createSetAccessorDeclaration(modifiers, name, typeParameters, parameters, type, body) : node; } -export function updateIndexSignatureDeclaration(node: IndexSignatureDeclaration, modifiers: readonly ModifierLike[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode): IndexSignatureDeclaration { +export function updateIndexSignatureDeclaration(node: IndexSignatureDeclaration, modifiers: readonly ModifierLike[] | undefined, parameters: readonly ParameterDeclaration[], type?: TypeNode): IndexSignatureDeclaration { return node.modifiers !== modifiers || node.parameters !== parameters || node.type !== type ? createIndexSignatureDeclaration(modifiers, parameters, type) : node; } diff --git a/packages/typescript/test/sync/ast.test.ts b/packages/typescript/test/sync/ast.test.ts index 795965ee5423c..ce86c834401bb 100644 --- a/packages/typescript/test/sync/ast.test.ts +++ b/packages/typescript/test/sync/ast.test.ts @@ -54,6 +54,7 @@ import { createFunctionDeclaration, createIdentifier, createIfStatement, + createIndexSignatureDeclaration, createMissingDeclaration, createNodeArray, createNumericLiteral, @@ -108,6 +109,19 @@ describe("NodeObject + childrenIter", { concurrency }, () => { }); }); +test("index signatures allow an omitted type", () => { + using api = spawnAPI(); + const source = api.createSourceFile("/repro.ts", "interface I { [key: string]; next: number; }").sourceFile; + const declaration = source.statements[0]; + assert.ok(astModule.isInterfaceDeclaration(declaration)); + const signature = declaration.members[0]; + assert.ok(astModule.isIndexSignatureDeclaration(signature)); + const type: astModule.TypeNode | undefined = signature.type; + assert.strictEqual(type, undefined); + assert.strictEqual(createIndexSignatureDeclaration(undefined, signature.parameters).type, undefined); + assert.strictEqual(declaration.members.length, 2); +}); + describe("AST property contracts", { concurrency }, () => { test("absent parser children have optional types", () => { const absent: { diff --git a/tools/scripts/tsc/ast.json b/tools/scripts/tsc/ast.json index 83d1e1fa305fb..b79180fd41fd2 100644 --- a/tools/scripts/tsc/ast.json +++ b/tools/scripts/tsc/ast.json @@ -2497,7 +2497,7 @@ { "name": "Type", "inherited": true, - "optional": false + "optional": true } ] }, diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index bdedc411bdb9c..53621ed093cee 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -2834,6 +2834,8 @@ func (c *Checker) checkSignatureDeclaration(node *ast.Node) { c.error(node, diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type) case ast.KindCallSignature: c.error(node, diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type) + case ast.KindIndexSignature: + c.error(node, diagnostics.Index_signature_which_lacks_type_annotation_implicitly_has_an_any_type) } } if returnTypeNode != nil { diff --git a/tsc/internal/checker/grammarchecks.go b/tsc/internal/checker/grammarchecks.go index 93bed4b355296..f8997b8e361e1 100644 --- a/tsc/internal/checker/grammarchecks.go +++ b/tsc/internal/checker/grammarchecks.go @@ -831,9 +831,6 @@ func (c *Checker) checkGrammarIndexSignatureParameters(node *ast.IndexSignatureD if !everyType(t, c.isValidIndexKeyType) { return c.grammarErrorOnNode(parameter.Name(), diagnostics.An_index_signature_parameter_type_must_be_string_number_symbol_or_a_template_literal_type) } - if node.Type == nil { - return c.grammarErrorOnNode(node.AsNode(), diagnostics.An_index_signature_must_have_a_type_annotation) - } return false } diff --git a/tsc/internal/diagnostics/diagnosticMessages.generated.json b/tsc/internal/diagnostics/diagnosticMessages.generated.json index ae749e01b85b5..394005f1c22b6 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.generated.json +++ b/tsc/internal/diagnostics/diagnosticMessages.generated.json @@ -16,7 +16,6 @@ "An_index_signature_parameter_cannot_have_an_accessibility_modifier_1018": "An index signature parameter cannot have an accessibility modifier.", "An_index_signature_parameter_cannot_have_a_question_mark_1019": "An index signature parameter cannot have a question mark.", "An_index_signature_parameter_cannot_have_an_initializer_1020": "An index signature parameter cannot have an initializer.", - "An_index_signature_must_have_a_type_annotation_1021": "An index signature must have a type annotation.", "An_index_signature_parameter_must_have_a_type_annotation_1022": "An index signature parameter must have a type annotation.", "readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature_1024": "'readonly' modifier can only appear on a property declaration or index signature.", "An_index_signature_cannot_have_a_trailing_comma_1025": "An index signature cannot have a trailing comma.", @@ -1751,6 +1750,7 @@ "This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Use_an_as_expression_instead_7059": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.", "This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Add_a_trailing_comma_or_explicit_cons_7060": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma or explicit constraint.", "A_mapped_type_may_not_declare_properties_or_methods_7061": "A mapped type may not declare properties or methods.", + "Index_signature_which_lacks_type_annotation_implicitly_has_an_any_type_7062": "Index signature, which lacks type annotation, implicitly has an 'any' type.", "Declaration_emit_elides_private_members_but_0_refers_to_a_private_member_Write_an_explicit_type_here_7080": "Declaration emit elides private members, but '{0}' refers to a private member. Write an explicit type here.", "You_cannot_rename_this_element_8000": "You cannot rename this element.", "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001": "You cannot rename elements that are defined in the standard TypeScript library.", diff --git a/tsc/internal/diagnostics/diagnosticMessages.json b/tsc/internal/diagnostics/diagnosticMessages.json index d4bf59ab839de..192ecc22f145d 100644 --- a/tsc/internal/diagnostics/diagnosticMessages.json +++ b/tsc/internal/diagnostics/diagnosticMessages.json @@ -67,10 +67,6 @@ "category": "Error", "code": 1020 }, - "An index signature must have a type annotation.": { - "category": "Error", - "code": 1021 - }, "An index signature parameter must have a type annotation.": { "category": "Error", "code": 1022 @@ -7042,6 +7038,10 @@ "category": "Error", "code": 7061 }, + "Index signature, which lacks type annotation, implicitly has an 'any' type.": { + "category": "Error", + "code": 7062 + }, "Declaration emit elides private members, but '{0}' refers to a private member. Write an explicit type here.": { "category": "Error", "code": 7080 diff --git a/tsc/internal/diagnostics/diagnostics_generated.go b/tsc/internal/diagnostics/diagnostics_generated.go index 0e08cf23701b7..3c9854f8ecb01 100644 --- a/tsc/internal/diagnostics/diagnostics_generated.go +++ b/tsc/internal/diagnostics/diagnostics_generated.go @@ -36,8 +36,6 @@ var An_index_signature_parameter_cannot_have_a_question_mark = &Message{code: 10 var An_index_signature_parameter_cannot_have_an_initializer = &Message{code: 1020, category: CategoryError, key: "An_index_signature_parameter_cannot_have_an_initializer_1020", text: "An index signature parameter cannot have an initializer."} -var An_index_signature_must_have_a_type_annotation = &Message{code: 1021, category: CategoryError, key: "An_index_signature_must_have_a_type_annotation_1021", text: "An index signature must have a type annotation."} - var An_index_signature_parameter_must_have_a_type_annotation = &Message{code: 1022, category: CategoryError, key: "An_index_signature_parameter_must_have_a_type_annotation_1022", text: "An index signature parameter must have a type annotation."} var X_readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature = &Message{code: 1024, category: CategoryError, key: "readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature_1024", text: "'readonly' modifier can only appear on a property declaration or index signature."} @@ -3506,6 +3504,8 @@ var This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Add_a_trailin var A_mapped_type_may_not_declare_properties_or_methods = &Message{code: 7061, category: CategoryError, key: "A_mapped_type_may_not_declare_properties_or_methods_7061", text: "A mapped type may not declare properties or methods."} +var Index_signature_which_lacks_type_annotation_implicitly_has_an_any_type = &Message{code: 7062, category: CategoryError, key: "Index_signature_which_lacks_type_annotation_implicitly_has_an_any_type_7062", text: "Index signature, which lacks type annotation, implicitly has an 'any' type."} + var Declaration_emit_elides_private_members_but_0_refers_to_a_private_member_Write_an_explicit_type_here = &Message{code: 7080, category: CategoryError, key: "Declaration_emit_elides_private_members_but_0_refers_to_a_private_member_Write_an_explicit_type_here_7080", text: "Declaration emit elides private members, but '{0}' refers to a private member. Write an explicit type here."} var You_cannot_rename_this_element = &Message{code: 8000, category: CategoryError, key: "You_cannot_rename_this_element_8000", text: "You cannot rename this element."} @@ -4480,7 +4480,6 @@ var allMessages = [...]**Message{ &An_index_signature_parameter_cannot_have_an_accessibility_modifier, &An_index_signature_parameter_cannot_have_a_question_mark, &An_index_signature_parameter_cannot_have_an_initializer, - &An_index_signature_must_have_a_type_annotation, &An_index_signature_parameter_must_have_a_type_annotation, &X_readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature, &An_index_signature_cannot_have_a_trailing_comma, @@ -6215,6 +6214,7 @@ var allMessages = [...]**Message{ &This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Use_an_as_expression_instead, &This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Add_a_trailing_comma_or_explicit_constraint, &A_mapped_type_may_not_declare_properties_or_methods, + &Index_signature_which_lacks_type_annotation_implicitly_has_an_any_type, &Declaration_emit_elides_private_members_but_0_refers_to_a_private_member_Write_an_explicit_type_here, &You_cannot_rename_this_element, &You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library, diff --git a/tsc/internal/diagnostics/loc/cs-CZ.json.gz b/tsc/internal/diagnostics/loc/cs-CZ.json.gz index e7fc4cd38bae8..5a8a2f66e3120 100644 Binary files a/tsc/internal/diagnostics/loc/cs-CZ.json.gz and b/tsc/internal/diagnostics/loc/cs-CZ.json.gz differ diff --git a/tsc/internal/diagnostics/loc/de-DE.json.gz b/tsc/internal/diagnostics/loc/de-DE.json.gz index 7ca0434996b49..cc94cca803f58 100644 Binary files a/tsc/internal/diagnostics/loc/de-DE.json.gz and b/tsc/internal/diagnostics/loc/de-DE.json.gz differ diff --git a/tsc/internal/diagnostics/loc/es-ES.json.gz b/tsc/internal/diagnostics/loc/es-ES.json.gz index d856bcbcbc4d1..1890e00931de7 100644 Binary files a/tsc/internal/diagnostics/loc/es-ES.json.gz and b/tsc/internal/diagnostics/loc/es-ES.json.gz differ diff --git a/tsc/internal/diagnostics/loc/fr-FR.json.gz b/tsc/internal/diagnostics/loc/fr-FR.json.gz index 1b55112a59e26..d5ed3afd4c45d 100644 Binary files a/tsc/internal/diagnostics/loc/fr-FR.json.gz and b/tsc/internal/diagnostics/loc/fr-FR.json.gz differ diff --git a/tsc/internal/diagnostics/loc/it-IT.json.gz b/tsc/internal/diagnostics/loc/it-IT.json.gz index 8366cae3ef2e5..d512180a60ed7 100644 Binary files a/tsc/internal/diagnostics/loc/it-IT.json.gz and b/tsc/internal/diagnostics/loc/it-IT.json.gz differ diff --git a/tsc/internal/diagnostics/loc/ja-JP.json.gz b/tsc/internal/diagnostics/loc/ja-JP.json.gz index 83462c0e5a8d1..df30dedcf4836 100644 Binary files a/tsc/internal/diagnostics/loc/ja-JP.json.gz and b/tsc/internal/diagnostics/loc/ja-JP.json.gz differ diff --git a/tsc/internal/diagnostics/loc/ko-KR.json.gz b/tsc/internal/diagnostics/loc/ko-KR.json.gz index 0c46e16e45a07..e54c24733c932 100644 Binary files a/tsc/internal/diagnostics/loc/ko-KR.json.gz and b/tsc/internal/diagnostics/loc/ko-KR.json.gz differ diff --git a/tsc/internal/diagnostics/loc/pl-PL.json.gz b/tsc/internal/diagnostics/loc/pl-PL.json.gz index 36ccf0e7a1c7e..79cecff3e5340 100644 Binary files a/tsc/internal/diagnostics/loc/pl-PL.json.gz and b/tsc/internal/diagnostics/loc/pl-PL.json.gz differ diff --git a/tsc/internal/diagnostics/loc/pt-BR.json.gz b/tsc/internal/diagnostics/loc/pt-BR.json.gz index 9f1dbef5d4066..a056abdd16a52 100644 Binary files a/tsc/internal/diagnostics/loc/pt-BR.json.gz and b/tsc/internal/diagnostics/loc/pt-BR.json.gz differ diff --git a/tsc/internal/diagnostics/loc/ru-RU.json.gz b/tsc/internal/diagnostics/loc/ru-RU.json.gz index b22cdcce3c625..e0f9bbb684fc2 100644 Binary files a/tsc/internal/diagnostics/loc/ru-RU.json.gz and b/tsc/internal/diagnostics/loc/ru-RU.json.gz differ diff --git a/tsc/internal/diagnostics/loc/tr-TR.json.gz b/tsc/internal/diagnostics/loc/tr-TR.json.gz index 0bf72b0d65166..b08cebd3c5633 100644 Binary files a/tsc/internal/diagnostics/loc/tr-TR.json.gz and b/tsc/internal/diagnostics/loc/tr-TR.json.gz differ diff --git a/tsc/internal/diagnostics/loc/zh-CN.json.gz b/tsc/internal/diagnostics/loc/zh-CN.json.gz index 90902d6a434e3..24ff3aebed33f 100644 Binary files a/tsc/internal/diagnostics/loc/zh-CN.json.gz and b/tsc/internal/diagnostics/loc/zh-CN.json.gz differ diff --git a/tsc/internal/diagnostics/loc/zh-TW.json.gz b/tsc/internal/diagnostics/loc/zh-TW.json.gz index 2291815290199..e583b3b0b0450 100644 Binary files a/tsc/internal/diagnostics/loc/zh-TW.json.gz and b/tsc/internal/diagnostics/loc/zh-TW.json.gz differ diff --git a/tsc/testdata/baselines/reference/compiler/arraySigChecking.errors.txt b/tsc/testdata/baselines/reference/compiler/arraySigChecking.errors.txt index 74dbd3487bfe2..603b706b5c3f7 100644 --- a/tsc/testdata/baselines/reference/compiler/arraySigChecking.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/arraySigChecking.errors.txt @@ -1,10 +1,11 @@ +arraySigChecking.ts(11,16): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. arraySigChecking.ts(11,17): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. arraySigChecking.ts(18,27): error TS2322: Type 'void' is not assignable to type 'string'. arraySigChecking.ts(22,13): error TS2322: Type 'number' is not assignable to type 'number[]'. arraySigChecking.ts(22,16): error TS2322: Type 'number' is not assignable to type 'number[]'. -==== arraySigChecking.ts (4 errors) ==== +==== arraySigChecking.ts (5 errors) ==== declare namespace M { interface iBar { t: any; } interface iFoo extends iBar { @@ -16,6 +17,8 @@ arraySigChecking.ts(22,16): error TS2322: Type 'number' is not assignable to typ } var foo: { [index: any]; }; // expect an error here + ~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~~~~ !!! error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. } diff --git a/tsc/testdata/baselines/reference/compiler/giant.errors.txt b/tsc/testdata/baselines/reference/compiler/giant.errors.txt index 248efb5e0f8a7..4768a09ada236 100644 --- a/tsc/testdata/baselines/reference/compiler/giant.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/giant.errors.txt @@ -16,7 +16,6 @@ giant.ts(34,29): error TS1005: '{' expected. giant.ts(35,12): error TS2300: Duplicate identifier 'tgF'. giant.ts(36,16): error TS2300: Duplicate identifier 'tgF'. giant.ts(36,20): error TS1005: '{' expected. -giant.ts(62,5): error TS1021: An index signature must have a type annotation. giant.ts(63,6): error TS1096: An index signature must have exactly one parameter. giant.ts(76,5): error TS2386: Overload signatures must all be optional or required. giant.ts(87,16): error TS2300: Duplicate identifier 'pgF'. @@ -37,7 +36,6 @@ giant.ts(98,33): error TS1005: '{' expected. giant.ts(99,16): error TS2300: Duplicate identifier 'tgF'. giant.ts(100,20): error TS2300: Duplicate identifier 'tgF'. giant.ts(100,24): error TS1005: '{' expected. -giant.ts(126,9): error TS1021: An index signature must have a type annotation. giant.ts(127,10): error TS1096: An index signature must have exactly one parameter. giant.ts(140,9): error TS2386: Overload signatures must all be optional or required. giant.ts(154,39): error TS1183: An implementation cannot be declared in ambient contexts. @@ -59,7 +57,6 @@ giant.ts(177,33): error TS1005: '{' expected. giant.ts(178,16): error TS2300: Duplicate identifier 'tgF'. giant.ts(179,20): error TS2300: Duplicate identifier 'tgF'. giant.ts(179,24): error TS1005: '{' expected. -giant.ts(205,9): error TS1021: An index signature must have a type annotation. giant.ts(206,10): error TS1096: An index signature must have exactly one parameter. giant.ts(219,9): error TS2386: Overload signatures must all be optional or required. giant.ts(233,39): error TS1183: An implementation cannot be declared in ambient contexts. @@ -107,7 +104,6 @@ giant.ts(292,29): error TS1005: '{' expected. giant.ts(293,12): error TS2300: Duplicate identifier 'tgF'. giant.ts(294,16): error TS2300: Duplicate identifier 'tgF'. giant.ts(294,20): error TS1005: '{' expected. -giant.ts(320,5): error TS1021: An index signature must have a type annotation. giant.ts(321,6): error TS1096: An index signature must have exactly one parameter. giant.ts(334,5): error TS2386: Overload signatures must all be optional or required. giant.ts(345,16): error TS2300: Duplicate identifier 'pgF'. @@ -128,7 +124,6 @@ giant.ts(356,33): error TS1005: '{' expected. giant.ts(357,16): error TS2300: Duplicate identifier 'tgF'. giant.ts(358,20): error TS2300: Duplicate identifier 'tgF'. giant.ts(358,24): error TS1005: '{' expected. -giant.ts(384,9): error TS1021: An index signature must have a type annotation. giant.ts(385,10): error TS1096: An index signature must have exactly one parameter. giant.ts(398,9): error TS2386: Overload signatures must all be optional or required. giant.ts(412,39): error TS1183: An implementation cannot be declared in ambient contexts. @@ -150,7 +145,6 @@ giant.ts(435,33): error TS1005: '{' expected. giant.ts(436,16): error TS2300: Duplicate identifier 'tgF'. giant.ts(437,20): error TS2300: Duplicate identifier 'tgF'. giant.ts(437,24): error TS1005: '{' expected. -giant.ts(463,9): error TS1021: An index signature must have a type annotation. giant.ts(464,10): error TS1096: An index signature must have exactly one parameter. giant.ts(477,9): error TS2386: Overload signatures must all be optional or required. giant.ts(491,39): error TS1183: An implementation cannot be declared in ambient contexts. @@ -208,7 +202,6 @@ giant.ts(556,21): error TS1036: Statements are not allowed in ambient contexts. giant.ts(558,24): error TS1183: An implementation cannot be declared in ambient contexts. giant.ts(561,21): error TS1183: An implementation cannot be declared in ambient contexts. giant.ts(563,21): error TS1183: An implementation cannot be declared in ambient contexts. -giant.ts(588,9): error TS1021: An index signature must have a type annotation. giant.ts(589,10): error TS1096: An index signature must have exactly one parameter. giant.ts(602,9): error TS2386: Overload signatures must all be optional or required. giant.ts(606,22): error TS1183: An implementation cannot be declared in ambient contexts. @@ -223,7 +216,6 @@ giant.ts(621,26): error TS1183: An implementation cannot be declared in ambient giant.ts(623,24): error TS1183: An implementation cannot be declared in ambient contexts. giant.ts(626,21): error TS1183: An implementation cannot be declared in ambient contexts. giant.ts(628,21): error TS1183: An implementation cannot be declared in ambient contexts. -giant.ts(654,9): error TS1021: An index signature must have a type annotation. giant.ts(655,10): error TS1096: An index signature must have exactly one parameter. giant.ts(668,9): error TS2386: Overload signatures must all be optional or required. giant.ts(672,22): error TS1183: An implementation cannot be declared in ambient contexts. @@ -231,7 +223,7 @@ giant.ts(672,25): error TS1036: Statements are not allowed in ambient contexts. giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient contexts. -==== giant.ts (231 errors) ==== +==== giant.ts (223 errors) ==== /* Prefixes p -> public @@ -330,8 +322,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -436,8 +426,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -559,8 +547,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -770,8 +756,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -876,8 +860,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -999,8 +981,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -1240,8 +1220,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. @@ -1336,8 +1314,6 @@ giant.ts(676,30): error TS1183: An implementation cannot be declared in ambient //Index Signature [p]; [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).errors.txt b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).errors.txt new file mode 100644 index 0000000000000..86a0036e74609 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).errors.txt @@ -0,0 +1,74 @@ +indexSignatureMissingType.ts(33,1): error TS2542: Index signature in type 'C' only permits reading. +indexSignatureMissingType.ts(36,9): error TS1019: An index signature parameter cannot have a question mark. +indexSignatureMissingType.ts(40,6): error TS1017: An index signature cannot have a rest parameter. +indexSignatureMissingType.ts(44,6): error TS1096: An index signature must have exactly one parameter. +indexSignatureMissingType.ts(48,6): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. + + +==== indexSignatureMissingType.ts (5 errors) ==== + export interface I { + [key: string]; + } + + export class C { + readonly [key: number]; + } + + export type T = { + [key: string] + property: number; + }; + + export interface NumberIndex { [key: number]; } + export interface SymbolIndex { [key: symbol]; } + export interface TemplateIndex { [key: `prefix-${string}`]; } + export interface ExplicitAny { [key: string]: any; } + export interface ExplicitUnknown { [key: string]: unknown; } + + declare const value: I; + export const asNumber: number = value.anyKey; + export const asString: string = value.anyKey; + export const indexedValue = value.anyKey; + + declare const instance: C; + declare const numbers: NumberIndex; + declare const symbols: SymbolIndex; + declare const templates: TemplateIndex; + export const classValue = instance[0]; + export const numberValue = numbers[0]; + export const symbolValue = symbols[Symbol.iterator]; + export const templateValue = templates["prefix-foo"]; + instance[0] = 1; + ~~~~~~~~~~~ +!!! error TS2542: Index signature in type 'C' only permits reading. + + interface OptionalParameter { + [key?: string]; + ~ +!!! error TS1019: An index signature parameter cannot have a question mark. + } + + interface RestParameter { + [...key: string[]]; + ~~~ +!!! error TS1017: An index signature cannot have a rest parameter. + } + + interface MultipleParameters { + [key: string, other: number]; + ~~~ +!!! error TS1096: An index signature must have exactly one parameter. + } + + interface InvalidKey { + [key: boolean]; + ~~~ +!!! error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. + } + + // @ts-ignore + interface Ignored { [key: string]; } + + // @ts-expect-error + interface Expected { [key: boolean]; } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).js b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).js new file mode 100644 index 0000000000000..4cbf08f85a90c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).js @@ -0,0 +1,106 @@ +//// [tests/cases/compiler/indexSignatureMissingType.ts] //// + +//// [indexSignatureMissingType.ts] +export interface I { + [key: string]; +} + +export class C { + readonly [key: number]; +} + +export type T = { + [key: string] + property: number; +}; + +export interface NumberIndex { [key: number]; } +export interface SymbolIndex { [key: symbol]; } +export interface TemplateIndex { [key: `prefix-${string}`]; } +export interface ExplicitAny { [key: string]: any; } +export interface ExplicitUnknown { [key: string]: unknown; } + +declare const value: I; +export const asNumber: number = value.anyKey; +export const asString: string = value.anyKey; +export const indexedValue = value.anyKey; + +declare const instance: C; +declare const numbers: NumberIndex; +declare const symbols: SymbolIndex; +declare const templates: TemplateIndex; +export const classValue = instance[0]; +export const numberValue = numbers[0]; +export const symbolValue = symbols[Symbol.iterator]; +export const templateValue = templates["prefix-foo"]; +instance[0] = 1; + +interface OptionalParameter { + [key?: string]; +} + +interface RestParameter { + [...key: string[]]; +} + +interface MultipleParameters { + [key: string, other: number]; +} + +interface InvalidKey { + [key: boolean]; +} + +// @ts-ignore +interface Ignored { [key: string]; } + +// @ts-expect-error +interface Expected { [key: boolean]; } + + +//// [indexSignatureMissingType.js] +export class C { +} +export const asNumber = value.anyKey; +export const asString = value.anyKey; +export const indexedValue = value.anyKey; +export const classValue = instance[0]; +export const numberValue = numbers[0]; +export const symbolValue = symbols[Symbol.iterator]; +export const templateValue = templates["prefix-foo"]; +instance[0] = 1; + + +//// [indexSignatureMissingType.d.ts] +export interface I { + [key: string]: any; +} +export declare class C { + readonly [key: number]: any; +} +export type T = { + [key: string]: any; + property: number; +}; +export interface NumberIndex { + [key: number]: any; +} +export interface SymbolIndex { + [key: symbol]: any; +} +export interface TemplateIndex { + [key: `prefix-${string}`]: any; +} +export interface ExplicitAny { + [key: string]: any; +} +export interface ExplicitUnknown { + [key: string]: unknown; +} +export declare const asNumber: number; +export declare const asString: string; +export declare const indexedValue: any; +export declare const classValue: any; +export declare const numberValue: any; +export declare const symbolValue: any; +export declare const templateValue: any; diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).symbols b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).symbols new file mode 100644 index 0000000000000..8897bd254269d --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).symbols @@ -0,0 +1,147 @@ +//// [tests/cases/compiler/indexSignatureMissingType.ts] //// + +=== indexSignatureMissingType.ts === +export interface I { +>I : Symbol(I, Decl(indexSignatureMissingType.ts, 0, 0)) + + [key: string]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 1, 5)) +} + +export class C { +>C : Symbol(C, Decl(indexSignatureMissingType.ts, 2, 1)) + + readonly [key: number]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 5, 14)) +} + +export type T = { +>T : Symbol(T, Decl(indexSignatureMissingType.ts, 6, 1)) + + [key: string] +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 9, 5)) + + property: number; +>property : Symbol(property, Decl(indexSignatureMissingType.ts, 9, 17)) + +}; + +export interface NumberIndex { [key: number]; } +>NumberIndex : Symbol(NumberIndex, Decl(indexSignatureMissingType.ts, 11, 2)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 13, 32)) + +export interface SymbolIndex { [key: symbol]; } +>SymbolIndex : Symbol(SymbolIndex, Decl(indexSignatureMissingType.ts, 13, 47)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 14, 32)) + +export interface TemplateIndex { [key: `prefix-${string}`]; } +>TemplateIndex : Symbol(TemplateIndex, Decl(indexSignatureMissingType.ts, 14, 47)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 15, 34)) + +export interface ExplicitAny { [key: string]: any; } +>ExplicitAny : Symbol(ExplicitAny, Decl(indexSignatureMissingType.ts, 15, 61)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 16, 32)) + +export interface ExplicitUnknown { [key: string]: unknown; } +>ExplicitUnknown : Symbol(ExplicitUnknown, Decl(indexSignatureMissingType.ts, 16, 52)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 17, 36)) + +declare const value: I; +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>I : Symbol(I, Decl(indexSignatureMissingType.ts, 0, 0)) + +export const asNumber: number = value.anyKey; +>asNumber : Symbol(asNumber, Decl(indexSignatureMissingType.ts, 20, 12)) +>value.anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) + +export const asString: string = value.anyKey; +>asString : Symbol(asString, Decl(indexSignatureMissingType.ts, 21, 12)) +>value.anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) + +export const indexedValue = value.anyKey; +>indexedValue : Symbol(indexedValue, Decl(indexSignatureMissingType.ts, 22, 12)) +>value.anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) + +declare const instance: C; +>instance : Symbol(instance, Decl(indexSignatureMissingType.ts, 24, 13)) +>C : Symbol(C, Decl(indexSignatureMissingType.ts, 2, 1)) + +declare const numbers: NumberIndex; +>numbers : Symbol(numbers, Decl(indexSignatureMissingType.ts, 25, 13)) +>NumberIndex : Symbol(NumberIndex, Decl(indexSignatureMissingType.ts, 11, 2)) + +declare const symbols: SymbolIndex; +>symbols : Symbol(symbols, Decl(indexSignatureMissingType.ts, 26, 13)) +>SymbolIndex : Symbol(SymbolIndex, Decl(indexSignatureMissingType.ts, 13, 47)) + +declare const templates: TemplateIndex; +>templates : Symbol(templates, Decl(indexSignatureMissingType.ts, 27, 13)) +>TemplateIndex : Symbol(TemplateIndex, Decl(indexSignatureMissingType.ts, 14, 47)) + +export const classValue = instance[0]; +>classValue : Symbol(classValue, Decl(indexSignatureMissingType.ts, 28, 12)) +>instance : Symbol(instance, Decl(indexSignatureMissingType.ts, 24, 13)) + +export const numberValue = numbers[0]; +>numberValue : Symbol(numberValue, Decl(indexSignatureMissingType.ts, 29, 12)) +>numbers : Symbol(numbers, Decl(indexSignatureMissingType.ts, 25, 13)) + +export const symbolValue = symbols[Symbol.iterator]; +>symbolValue : Symbol(symbolValue, Decl(indexSignatureMissingType.ts, 30, 12)) +>symbols : Symbol(symbols, Decl(indexSignatureMissingType.ts, 26, 13)) +>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) + +export const templateValue = templates["prefix-foo"]; +>templateValue : Symbol(templateValue, Decl(indexSignatureMissingType.ts, 31, 12)) +>templates : Symbol(templates, Decl(indexSignatureMissingType.ts, 27, 13)) + +instance[0] = 1; +>instance : Symbol(instance, Decl(indexSignatureMissingType.ts, 24, 13)) + +interface OptionalParameter { +>OptionalParameter : Symbol(OptionalParameter, Decl(indexSignatureMissingType.ts, 32, 16)) + + [key?: string]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 35, 5)) +} + +interface RestParameter { +>RestParameter : Symbol(RestParameter, Decl(indexSignatureMissingType.ts, 36, 1)) + + [...key: string[]]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 39, 5)) +} + +interface MultipleParameters { +>MultipleParameters : Symbol(MultipleParameters, Decl(indexSignatureMissingType.ts, 40, 1)) + + [key: string, other: number]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 43, 5)) +>other : Symbol(other, Decl(indexSignatureMissingType.ts, 43, 17)) +} + +interface InvalidKey { +>InvalidKey : Symbol(InvalidKey, Decl(indexSignatureMissingType.ts, 44, 1)) + + [key: boolean]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 47, 5)) +} + +// @ts-ignore +interface Ignored { [key: string]; } +>Ignored : Symbol(Ignored, Decl(indexSignatureMissingType.ts, 48, 1)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 51, 21)) + +// @ts-expect-error +interface Expected { [key: boolean]; } +>Expected : Symbol(Expected, Decl(indexSignatureMissingType.ts, 51, 36)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 54, 22)) + diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).types b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).types new file mode 100644 index 0000000000000..5d3d1bcdb256b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=false).types @@ -0,0 +1,136 @@ +//// [tests/cases/compiler/indexSignatureMissingType.ts] //// + +=== indexSignatureMissingType.ts === +export interface I { + [key: string]; +>key : string +} + +export class C { +>C : C + + readonly [key: number]; +>key : number +} + +export type T = { +>T : T + + [key: string] +>key : string + + property: number; +>property : number + +}; + +export interface NumberIndex { [key: number]; } +>key : number + +export interface SymbolIndex { [key: symbol]; } +>key : symbol + +export interface TemplateIndex { [key: `prefix-${string}`]; } +>key : `prefix-${string}` + +export interface ExplicitAny { [key: string]: any; } +>key : string + +export interface ExplicitUnknown { [key: string]: unknown; } +>key : string + +declare const value: I; +>value : I + +export const asNumber: number = value.anyKey; +>asNumber : number +>value.anyKey : any +>value : I +>anyKey : any + +export const asString: string = value.anyKey; +>asString : string +>value.anyKey : any +>value : I +>anyKey : any + +export const indexedValue = value.anyKey; +>indexedValue : any +>value.anyKey : any +>value : I +>anyKey : any + +declare const instance: C; +>instance : C + +declare const numbers: NumberIndex; +>numbers : NumberIndex + +declare const symbols: SymbolIndex; +>symbols : SymbolIndex + +declare const templates: TemplateIndex; +>templates : TemplateIndex + +export const classValue = instance[0]; +>classValue : any +>instance[0] : any +>instance : C +>0 : 0 + +export const numberValue = numbers[0]; +>numberValue : any +>numbers[0] : any +>numbers : NumberIndex +>0 : 0 + +export const symbolValue = symbols[Symbol.iterator]; +>symbolValue : any +>symbols[Symbol.iterator] : any +>symbols : SymbolIndex +>Symbol.iterator : unique symbol +>Symbol : SymbolConstructor +>iterator : unique symbol + +export const templateValue = templates["prefix-foo"]; +>templateValue : any +>templates["prefix-foo"] : any +>templates : TemplateIndex +>"prefix-foo" : "prefix-foo" + +instance[0] = 1; +>instance[0] = 1 : 1 +>instance[0] : any +>instance : C +>0 : 0 +>1 : 1 + +interface OptionalParameter { + [key?: string]; +>key : string | undefined +} + +interface RestParameter { + [...key: string[]]; +>key : string[] +} + +interface MultipleParameters { + [key: string, other: number]; +>key : string +>other : number +} + +interface InvalidKey { + [key: boolean]; +>key : boolean +} + +// @ts-ignore +interface Ignored { [key: string]; } +>key : string + +// @ts-expect-error +interface Expected { [key: boolean]; } +>key : boolean + diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).errors.txt b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).errors.txt new file mode 100644 index 0000000000000..5e3a9f67445ef --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).errors.txt @@ -0,0 +1,104 @@ +indexSignatureMissingType.ts(2,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(6,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(10,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(14,32): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(15,32): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(16,34): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(33,1): error TS2542: Index signature in type 'C' only permits reading. +indexSignatureMissingType.ts(36,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(36,9): error TS1019: An index signature parameter cannot have a question mark. +indexSignatureMissingType.ts(40,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(40,6): error TS1017: An index signature cannot have a rest parameter. +indexSignatureMissingType.ts(44,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(44,6): error TS1096: An index signature must have exactly one parameter. +indexSignatureMissingType.ts(48,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. +indexSignatureMissingType.ts(48,6): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. + + +==== indexSignatureMissingType.ts (15 errors) ==== + export interface I { + [key: string]; + ~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + } + + export class C { + readonly [key: number]; + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + } + + export type T = { + [key: string] + ~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + property: number; + }; + + export interface NumberIndex { [key: number]; } + ~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + export interface SymbolIndex { [key: symbol]; } + ~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + export interface TemplateIndex { [key: `prefix-${string}`]; } + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + export interface ExplicitAny { [key: string]: any; } + export interface ExplicitUnknown { [key: string]: unknown; } + + declare const value: I; + export const asNumber: number = value.anyKey; + export const asString: string = value.anyKey; + export const indexedValue = value.anyKey; + + declare const instance: C; + declare const numbers: NumberIndex; + declare const symbols: SymbolIndex; + declare const templates: TemplateIndex; + export const classValue = instance[0]; + export const numberValue = numbers[0]; + export const symbolValue = symbols[Symbol.iterator]; + export const templateValue = templates["prefix-foo"]; + instance[0] = 1; + ~~~~~~~~~~~ +!!! error TS2542: Index signature in type 'C' only permits reading. + + interface OptionalParameter { + [key?: string]; + ~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + ~ +!!! error TS1019: An index signature parameter cannot have a question mark. + } + + interface RestParameter { + [...key: string[]]; + ~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + ~~~ +!!! error TS1017: An index signature cannot have a rest parameter. + } + + interface MultipleParameters { + [key: string, other: number]; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + ~~~ +!!! error TS1096: An index signature must have exactly one parameter. + } + + interface InvalidKey { + [key: boolean]; + ~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. + ~~~ +!!! error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. + } + + // @ts-ignore + interface Ignored { [key: string]; } + + // @ts-expect-error + interface Expected { [key: boolean]; } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).js b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).js new file mode 100644 index 0000000000000..4cbf08f85a90c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).js @@ -0,0 +1,106 @@ +//// [tests/cases/compiler/indexSignatureMissingType.ts] //// + +//// [indexSignatureMissingType.ts] +export interface I { + [key: string]; +} + +export class C { + readonly [key: number]; +} + +export type T = { + [key: string] + property: number; +}; + +export interface NumberIndex { [key: number]; } +export interface SymbolIndex { [key: symbol]; } +export interface TemplateIndex { [key: `prefix-${string}`]; } +export interface ExplicitAny { [key: string]: any; } +export interface ExplicitUnknown { [key: string]: unknown; } + +declare const value: I; +export const asNumber: number = value.anyKey; +export const asString: string = value.anyKey; +export const indexedValue = value.anyKey; + +declare const instance: C; +declare const numbers: NumberIndex; +declare const symbols: SymbolIndex; +declare const templates: TemplateIndex; +export const classValue = instance[0]; +export const numberValue = numbers[0]; +export const symbolValue = symbols[Symbol.iterator]; +export const templateValue = templates["prefix-foo"]; +instance[0] = 1; + +interface OptionalParameter { + [key?: string]; +} + +interface RestParameter { + [...key: string[]]; +} + +interface MultipleParameters { + [key: string, other: number]; +} + +interface InvalidKey { + [key: boolean]; +} + +// @ts-ignore +interface Ignored { [key: string]; } + +// @ts-expect-error +interface Expected { [key: boolean]; } + + +//// [indexSignatureMissingType.js] +export class C { +} +export const asNumber = value.anyKey; +export const asString = value.anyKey; +export const indexedValue = value.anyKey; +export const classValue = instance[0]; +export const numberValue = numbers[0]; +export const symbolValue = symbols[Symbol.iterator]; +export const templateValue = templates["prefix-foo"]; +instance[0] = 1; + + +//// [indexSignatureMissingType.d.ts] +export interface I { + [key: string]: any; +} +export declare class C { + readonly [key: number]: any; +} +export type T = { + [key: string]: any; + property: number; +}; +export interface NumberIndex { + [key: number]: any; +} +export interface SymbolIndex { + [key: symbol]: any; +} +export interface TemplateIndex { + [key: `prefix-${string}`]: any; +} +export interface ExplicitAny { + [key: string]: any; +} +export interface ExplicitUnknown { + [key: string]: unknown; +} +export declare const asNumber: number; +export declare const asString: string; +export declare const indexedValue: any; +export declare const classValue: any; +export declare const numberValue: any; +export declare const symbolValue: any; +export declare const templateValue: any; diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).symbols b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).symbols new file mode 100644 index 0000000000000..8897bd254269d --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).symbols @@ -0,0 +1,147 @@ +//// [tests/cases/compiler/indexSignatureMissingType.ts] //// + +=== indexSignatureMissingType.ts === +export interface I { +>I : Symbol(I, Decl(indexSignatureMissingType.ts, 0, 0)) + + [key: string]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 1, 5)) +} + +export class C { +>C : Symbol(C, Decl(indexSignatureMissingType.ts, 2, 1)) + + readonly [key: number]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 5, 14)) +} + +export type T = { +>T : Symbol(T, Decl(indexSignatureMissingType.ts, 6, 1)) + + [key: string] +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 9, 5)) + + property: number; +>property : Symbol(property, Decl(indexSignatureMissingType.ts, 9, 17)) + +}; + +export interface NumberIndex { [key: number]; } +>NumberIndex : Symbol(NumberIndex, Decl(indexSignatureMissingType.ts, 11, 2)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 13, 32)) + +export interface SymbolIndex { [key: symbol]; } +>SymbolIndex : Symbol(SymbolIndex, Decl(indexSignatureMissingType.ts, 13, 47)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 14, 32)) + +export interface TemplateIndex { [key: `prefix-${string}`]; } +>TemplateIndex : Symbol(TemplateIndex, Decl(indexSignatureMissingType.ts, 14, 47)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 15, 34)) + +export interface ExplicitAny { [key: string]: any; } +>ExplicitAny : Symbol(ExplicitAny, Decl(indexSignatureMissingType.ts, 15, 61)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 16, 32)) + +export interface ExplicitUnknown { [key: string]: unknown; } +>ExplicitUnknown : Symbol(ExplicitUnknown, Decl(indexSignatureMissingType.ts, 16, 52)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 17, 36)) + +declare const value: I; +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>I : Symbol(I, Decl(indexSignatureMissingType.ts, 0, 0)) + +export const asNumber: number = value.anyKey; +>asNumber : Symbol(asNumber, Decl(indexSignatureMissingType.ts, 20, 12)) +>value.anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) + +export const asString: string = value.anyKey; +>asString : Symbol(asString, Decl(indexSignatureMissingType.ts, 21, 12)) +>value.anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) + +export const indexedValue = value.anyKey; +>indexedValue : Symbol(indexedValue, Decl(indexSignatureMissingType.ts, 22, 12)) +>value.anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) +>value : Symbol(value, Decl(indexSignatureMissingType.ts, 19, 13)) +>anyKey : Symbol(I.__index, Decl(indexSignatureMissingType.ts, 0, 20)) + +declare const instance: C; +>instance : Symbol(instance, Decl(indexSignatureMissingType.ts, 24, 13)) +>C : Symbol(C, Decl(indexSignatureMissingType.ts, 2, 1)) + +declare const numbers: NumberIndex; +>numbers : Symbol(numbers, Decl(indexSignatureMissingType.ts, 25, 13)) +>NumberIndex : Symbol(NumberIndex, Decl(indexSignatureMissingType.ts, 11, 2)) + +declare const symbols: SymbolIndex; +>symbols : Symbol(symbols, Decl(indexSignatureMissingType.ts, 26, 13)) +>SymbolIndex : Symbol(SymbolIndex, Decl(indexSignatureMissingType.ts, 13, 47)) + +declare const templates: TemplateIndex; +>templates : Symbol(templates, Decl(indexSignatureMissingType.ts, 27, 13)) +>TemplateIndex : Symbol(TemplateIndex, Decl(indexSignatureMissingType.ts, 14, 47)) + +export const classValue = instance[0]; +>classValue : Symbol(classValue, Decl(indexSignatureMissingType.ts, 28, 12)) +>instance : Symbol(instance, Decl(indexSignatureMissingType.ts, 24, 13)) + +export const numberValue = numbers[0]; +>numberValue : Symbol(numberValue, Decl(indexSignatureMissingType.ts, 29, 12)) +>numbers : Symbol(numbers, Decl(indexSignatureMissingType.ts, 25, 13)) + +export const symbolValue = symbols[Symbol.iterator]; +>symbolValue : Symbol(symbolValue, Decl(indexSignatureMissingType.ts, 30, 12)) +>symbols : Symbol(symbols, Decl(indexSignatureMissingType.ts, 26, 13)) +>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) + +export const templateValue = templates["prefix-foo"]; +>templateValue : Symbol(templateValue, Decl(indexSignatureMissingType.ts, 31, 12)) +>templates : Symbol(templates, Decl(indexSignatureMissingType.ts, 27, 13)) + +instance[0] = 1; +>instance : Symbol(instance, Decl(indexSignatureMissingType.ts, 24, 13)) + +interface OptionalParameter { +>OptionalParameter : Symbol(OptionalParameter, Decl(indexSignatureMissingType.ts, 32, 16)) + + [key?: string]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 35, 5)) +} + +interface RestParameter { +>RestParameter : Symbol(RestParameter, Decl(indexSignatureMissingType.ts, 36, 1)) + + [...key: string[]]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 39, 5)) +} + +interface MultipleParameters { +>MultipleParameters : Symbol(MultipleParameters, Decl(indexSignatureMissingType.ts, 40, 1)) + + [key: string, other: number]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 43, 5)) +>other : Symbol(other, Decl(indexSignatureMissingType.ts, 43, 17)) +} + +interface InvalidKey { +>InvalidKey : Symbol(InvalidKey, Decl(indexSignatureMissingType.ts, 44, 1)) + + [key: boolean]; +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 47, 5)) +} + +// @ts-ignore +interface Ignored { [key: string]; } +>Ignored : Symbol(Ignored, Decl(indexSignatureMissingType.ts, 48, 1)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 51, 21)) + +// @ts-expect-error +interface Expected { [key: boolean]; } +>Expected : Symbol(Expected, Decl(indexSignatureMissingType.ts, 51, 36)) +>key : Symbol(key, Decl(indexSignatureMissingType.ts, 54, 22)) + diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).types b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).types new file mode 100644 index 0000000000000..5d3d1bcdb256b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMissingType(noimplicitany=true).types @@ -0,0 +1,136 @@ +//// [tests/cases/compiler/indexSignatureMissingType.ts] //// + +=== indexSignatureMissingType.ts === +export interface I { + [key: string]; +>key : string +} + +export class C { +>C : C + + readonly [key: number]; +>key : number +} + +export type T = { +>T : T + + [key: string] +>key : string + + property: number; +>property : number + +}; + +export interface NumberIndex { [key: number]; } +>key : number + +export interface SymbolIndex { [key: symbol]; } +>key : symbol + +export interface TemplateIndex { [key: `prefix-${string}`]; } +>key : `prefix-${string}` + +export interface ExplicitAny { [key: string]: any; } +>key : string + +export interface ExplicitUnknown { [key: string]: unknown; } +>key : string + +declare const value: I; +>value : I + +export const asNumber: number = value.anyKey; +>asNumber : number +>value.anyKey : any +>value : I +>anyKey : any + +export const asString: string = value.anyKey; +>asString : string +>value.anyKey : any +>value : I +>anyKey : any + +export const indexedValue = value.anyKey; +>indexedValue : any +>value.anyKey : any +>value : I +>anyKey : any + +declare const instance: C; +>instance : C + +declare const numbers: NumberIndex; +>numbers : NumberIndex + +declare const symbols: SymbolIndex; +>symbols : SymbolIndex + +declare const templates: TemplateIndex; +>templates : TemplateIndex + +export const classValue = instance[0]; +>classValue : any +>instance[0] : any +>instance : C +>0 : 0 + +export const numberValue = numbers[0]; +>numberValue : any +>numbers[0] : any +>numbers : NumberIndex +>0 : 0 + +export const symbolValue = symbols[Symbol.iterator]; +>symbolValue : any +>symbols[Symbol.iterator] : any +>symbols : SymbolIndex +>Symbol.iterator : unique symbol +>Symbol : SymbolConstructor +>iterator : unique symbol + +export const templateValue = templates["prefix-foo"]; +>templateValue : any +>templates["prefix-foo"] : any +>templates : TemplateIndex +>"prefix-foo" : "prefix-foo" + +instance[0] = 1; +>instance[0] = 1 : 1 +>instance[0] : any +>instance : C +>0 : 0 +>1 : 1 + +interface OptionalParameter { + [key?: string]; +>key : string | undefined +} + +interface RestParameter { + [...key: string[]]; +>key : string[] +} + +interface MultipleParameters { + [key: string, other: number]; +>key : string +>other : number +} + +interface InvalidKey { + [key: boolean]; +>key : boolean +} + +// @ts-ignore +interface Ignored { [key: string]; } +>key : string + +// @ts-expect-error +interface Expected { [key: boolean]; } +>key : boolean + diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureMustHaveTypeAnnotation.errors.txt b/tsc/testdata/baselines/reference/compiler/indexSignatureMustHaveTypeAnnotation.errors.txt index b9cc9dfed62af..37805c10625db 100644 --- a/tsc/testdata/baselines/reference/compiler/indexSignatureMustHaveTypeAnnotation.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureMustHaveTypeAnnotation.errors.txt @@ -1,8 +1,8 @@ indexSignatureMustHaveTypeAnnotation.ts(3,6): error TS2304: Cannot find name 'x'. -indexSignatureMustHaveTypeAnnotation.ts(4,5): error TS1021: An index signature must have a type annotation. +indexSignatureMustHaveTypeAnnotation.ts(4,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureMustHaveTypeAnnotation.ts(9,5): error TS2564: Property '[x]' has no initializer and is not definitely assigned in the constructor. indexSignatureMustHaveTypeAnnotation.ts(9,6): error TS2304: Cannot find name 'x'. -indexSignatureMustHaveTypeAnnotation.ts(14,5): error TS1021: An index signature must have a type annotation. +indexSignatureMustHaveTypeAnnotation.ts(14,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ==== indexSignatureMustHaveTypeAnnotation.ts (5 errors) ==== @@ -13,7 +13,7 @@ indexSignatureMustHaveTypeAnnotation.ts(14,5): error TS1021: An index signature !!! error TS2304: Cannot find name 'x'. [x: string]; ~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. } class C { @@ -29,5 +29,5 @@ indexSignatureMustHaveTypeAnnotation.ts(14,5): error TS1021: An index signature class C2 { [x: string] ~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck.errors.txt b/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck.errors.txt index 1633aa08f67c6..74e70e81eeb73 100644 --- a/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck.errors.txt @@ -1,11 +1,15 @@ indexSignatureTypeCheck.ts(8,5): error TS2322: Type 'null' is not assignable to type 'IPropertySet'. +indexSignatureTypeCheck.ts(14,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck.ts(14,8): error TS1019: An index signature parameter cannot have a question mark. +indexSignatureTypeCheck.ts(15,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck.ts(15,6): error TS1017: An index signature cannot have a rest parameter. +indexSignatureTypeCheck.ts(16,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck.ts(16,6): error TS1096: An index signature must have exactly one parameter. +indexSignatureTypeCheck.ts(17,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck.ts(17,6): error TS1096: An index signature must have exactly one parameter. -==== indexSignatureTypeCheck.ts (5 errors) ==== +==== indexSignatureTypeCheck.ts (9 errors) ==== interface IPropertySet { [index: string]: any; @@ -22,15 +26,23 @@ indexSignatureTypeCheck.ts(17,6): error TS1096: An index signature must have exa interface indexErrors { [p2?: string]; + ~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~ !!! error TS1019: An index signature parameter cannot have a question mark. [...p3: any[]]; + ~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~~ !!! error TS1017: An index signature cannot have a rest parameter. [p4: string, p5?: string]; + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~ !!! error TS1096: An index signature must have exactly one parameter. [p6: string, ...p7: any[]]; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~ !!! error TS1096: An index signature must have exactly one parameter. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck2.errors.txt b/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck2.errors.txt index 43c17fdc25b73..02fe24407c45a 100644 --- a/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck2.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureTypeCheck2.errors.txt @@ -1,11 +1,15 @@ indexSignatureTypeCheck2.ts(5,5): error TS2322: Type 'null' is not assignable to type 'IPropertySet'. +indexSignatureTypeCheck2.ts(10,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck2.ts(10,8): error TS1019: An index signature parameter cannot have a question mark. +indexSignatureTypeCheck2.ts(11,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck2.ts(11,6): error TS1017: An index signature cannot have a rest parameter. +indexSignatureTypeCheck2.ts(12,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck2.ts(12,6): error TS1096: An index signature must have exactly one parameter. +indexSignatureTypeCheck2.ts(13,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. indexSignatureTypeCheck2.ts(13,6): error TS1096: An index signature must have exactly one parameter. -==== indexSignatureTypeCheck2.ts (5 errors) ==== +==== indexSignatureTypeCheck2.ts (9 errors) ==== class IPropertySet { [index: string]: any } @@ -18,15 +22,23 @@ indexSignatureTypeCheck2.ts(13,6): error TS1096: An index signature must have ex interface indexErrors { [p2?: string]; + ~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~ !!! error TS1019: An index signature parameter cannot have a question mark. [...p3: any[]]; + ~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~~ !!! error TS1017: An index signature cannot have a rest parameter. [p4: string, p5?: string]; + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~ !!! error TS1096: An index signature must have exactly one parameter. [p6: string, ...p7: any[]]; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~ !!! error TS1096: An index signature must have exactly one parameter. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexSignatureWithoutTypeAnnotation1.errors.txt b/tsc/testdata/baselines/reference/compiler/indexSignatureWithoutTypeAnnotation1.errors.txt index 8e977b5799d36..83028fec60233 100644 --- a/tsc/testdata/baselines/reference/compiler/indexSignatureWithoutTypeAnnotation1.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexSignatureWithoutTypeAnnotation1.errors.txt @@ -1,9 +1,9 @@ -indexSignatureWithoutTypeAnnotation1.ts(2,3): error TS1021: An index signature must have a type annotation. +indexSignatureWithoutTypeAnnotation1.ts(2,3): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ==== indexSignatureWithoutTypeAnnotation1.ts (1 errors) ==== class C { [a: number]; ~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexTypeCheck.errors.txt b/tsc/testdata/baselines/reference/compiler/indexTypeCheck.errors.txt index ccf0c99e3919d..27cbdd698b718 100644 --- a/tsc/testdata/baselines/reference/compiler/indexTypeCheck.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexTypeCheck.errors.txt @@ -1,5 +1,3 @@ -indexTypeCheck.ts(2,2): error TS1021: An index signature must have a type annotation. -indexTypeCheck.ts(3,2): error TS1021: An index signature must have a type annotation. indexTypeCheck.ts(17,2): error TS2413: 'number' index type 'number' is not assignable to 'string' index type 'string'. indexTypeCheck.ts(22,2): error TS2413: 'number' index type 'Orange' is not assignable to 'string' index type 'Yellow'. indexTypeCheck.ts(27,2): error TS2413: 'number' index type 'number' is not assignable to 'string' index type 'string'. @@ -8,14 +6,10 @@ indexTypeCheck.ts(36,3): error TS1268: An index signature parameter type must be indexTypeCheck.ts(51,8): error TS2538: Type 'Blue' cannot be used as an index type. -==== indexTypeCheck.ts (8 errors) ==== +==== indexTypeCheck.ts (6 errors) ==== interface Red { [n:number]; // ok - ~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [s:string]; // ok - ~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. } interface Blue { diff --git a/tsc/testdata/baselines/reference/compiler/indexWithoutParamType.errors.txt b/tsc/testdata/baselines/reference/compiler/indexWithoutParamType.errors.txt index 1483b3eb8b537..a39423b96148a 100644 --- a/tsc/testdata/baselines/reference/compiler/indexWithoutParamType.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexWithoutParamType.errors.txt @@ -1,7 +1,10 @@ indexWithoutParamType.ts(1,10): error TS1096: An index signature must have exactly one parameter. +indexWithoutParamType.ts(1,10): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. -==== indexWithoutParamType.ts (1 errors) ==== +==== indexWithoutParamType.ts (2 errors) ==== var y: { []; } // Error ~~~ -!!! error TS1096: An index signature must have exactly one parameter. \ No newline at end of file +!!! error TS1096: An index signature must have exactly one parameter. + ~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexerConstraints2.errors.txt b/tsc/testdata/baselines/reference/compiler/indexerConstraints2.errors.txt index ce0ebe20eb7ff..37c794986c319 100644 --- a/tsc/testdata/baselines/reference/compiler/indexerConstraints2.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/indexerConstraints2.errors.txt @@ -8,7 +8,7 @@ indexerConstraints2.ts(52,6): error TS1337: An index signature parameter type ca indexerConstraints2.ts(58,6): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. indexerConstraints2.ts(70,6): error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead. indexerConstraints2.ts(74,6): error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead. -indexerConstraints2.ts(79,5): error TS1021: An index signature must have a type annotation. +indexerConstraints2.ts(79,5): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ==== indexerConstraints2.ts (11 errors) ==== @@ -112,6 +112,6 @@ indexerConstraints2.ts(79,5): error TS1021: An index signature must have a type interface T { [key: Key] ~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/parserIndexSignature11.errors.txt b/tsc/testdata/baselines/reference/conformance/parserIndexSignature11.errors.txt index 3e95b906714b6..07a90124afc21 100644 --- a/tsc/testdata/baselines/reference/conformance/parserIndexSignature11.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/parserIndexSignature11.errors.txt @@ -1,16 +1,13 @@ parserIndexSignature11.ts(2,10): error TS2304: Cannot find name 'p'. -parserIndexSignature11.ts(3,9): error TS1021: An index signature must have a type annotation. parserIndexSignature11.ts(4,10): error TS1096: An index signature must have exactly one parameter. -==== parserIndexSignature11.ts (3 errors) ==== +==== parserIndexSignature11.ts (2 errors) ==== interface I { [p]; // Used to be indexer, now it is a computed property ~ !!! error TS2304: Cannot find name 'p'. [p1: string]; - ~~~~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ !!! error TS1096: An index signature must have exactly one parameter. diff --git a/tsc/testdata/baselines/reference/conformance/parserIndexSignature6.errors.txt b/tsc/testdata/baselines/reference/conformance/parserIndexSignature6.errors.txt index c6f0788b6ffca..ddab49bda6add 100644 --- a/tsc/testdata/baselines/reference/conformance/parserIndexSignature6.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/parserIndexSignature6.errors.txt @@ -1,9 +1,12 @@ +parserIndexSignature6.ts(2,3): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. parserIndexSignature6.ts(2,4): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. -==== parserIndexSignature6.ts (1 errors) ==== +==== parserIndexSignature6.ts (2 errors) ==== interface I { [a:boolean] + ~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~ !!! error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/parserIndexSignature7.errors.txt b/tsc/testdata/baselines/reference/conformance/parserIndexSignature7.errors.txt index 6aac68380f312..c7ecf13094cf7 100644 --- a/tsc/testdata/baselines/reference/conformance/parserIndexSignature7.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/parserIndexSignature7.errors.txt @@ -1,9 +1,9 @@ -parserIndexSignature7.ts(2,3): error TS1021: An index signature must have a type annotation. +parserIndexSignature7.ts(2,3): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ==== parserIndexSignature7.ts (1 errors) ==== interface I { [a:string] ~~~~~~~~~~ -!!! error TS1021: An index signature must have a type annotation. +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/parserIndexSignature8.errors.txt b/tsc/testdata/baselines/reference/conformance/parserIndexSignature8.errors.txt index 44bd4aeeb92c6..b132de1e7be5a 100644 --- a/tsc/testdata/baselines/reference/conformance/parserIndexSignature8.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/parserIndexSignature8.errors.txt @@ -1,12 +1,18 @@ +parserIndexSignature8.ts(1,12): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. parserIndexSignature8.ts(1,13): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. +parserIndexSignature8.ts(2,13): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. parserIndexSignature8.ts(2,14): error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. -==== parserIndexSignature8.ts (2 errors) ==== +==== parserIndexSignature8.ts (4 errors) ==== var foo: { [index: any]; }; // expect an error here + ~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~~~~ !!! error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. var foo2: { [index: RegExp]; }; // expect an error here + ~~~~~~~~~~~~~~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. ~~~~~ !!! error TS1268: An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/parserObjectType6.errors.txt b/tsc/testdata/baselines/reference/conformance/parserObjectType6.errors.txt index aa8fe99484a1b..e2eaeb66b104c 100644 --- a/tsc/testdata/baselines/reference/conformance/parserObjectType6.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/parserObjectType6.errors.txt @@ -1,8 +1,9 @@ parserObjectType6.ts(2,7): error TS2304: Cannot find name 'B'. parserObjectType6.ts(3,4): error TS1096: An index signature must have exactly one parameter. +parserObjectType6.ts(3,4): error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. -==== parserObjectType6.ts (2 errors) ==== +==== parserObjectType6.ts (3 errors) ==== var v: { a: B ~ @@ -10,4 +11,6 @@ parserObjectType6.ts(3,4): error TS1096: An index signature must have exactly on []; ~~~ !!! error TS1096: An index signature must have exactly one parameter. + ~~~ +!!! error TS7062: Index signature, which lacks type annotation, implicitly has an 'any' type. }; \ No newline at end of file diff --git a/tsc/testdata/tests/cases/compiler/indexSignatureMissingType.ts b/tsc/testdata/tests/cases/compiler/indexSignatureMissingType.ts new file mode 100644 index 0000000000000..e5f82c0756b04 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/indexSignatureMissingType.ts @@ -0,0 +1,59 @@ +// @declaration: true +// @noImplicitAny: false, true +// @target: esnext + +export interface I { + [key: string]; +} + +export class C { + readonly [key: number]; +} + +export type T = { + [key: string] + property: number; +}; + +export interface NumberIndex { [key: number]; } +export interface SymbolIndex { [key: symbol]; } +export interface TemplateIndex { [key: `prefix-${string}`]; } +export interface ExplicitAny { [key: string]: any; } +export interface ExplicitUnknown { [key: string]: unknown; } + +declare const value: I; +export const asNumber: number = value.anyKey; +export const asString: string = value.anyKey; +export const indexedValue = value.anyKey; + +declare const instance: C; +declare const numbers: NumberIndex; +declare const symbols: SymbolIndex; +declare const templates: TemplateIndex; +export const classValue = instance[0]; +export const numberValue = numbers[0]; +export const symbolValue = symbols[Symbol.iterator]; +export const templateValue = templates["prefix-foo"]; +instance[0] = 1; + +interface OptionalParameter { + [key?: string]; +} + +interface RestParameter { + [...key: string[]]; +} + +interface MultipleParameters { + [key: string, other: number]; +} + +interface InvalidKey { + [key: boolean]; +} + +// @ts-ignore +interface Ignored { [key: string]; } + +// @ts-expect-error +interface Expected { [key: boolean]; }