Skip to content
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
1 change: 0 additions & 1 deletion packages/typescript/src/ast/ast.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/src/ast/factory.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/typescript/test/sync/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
createFunctionDeclaration,
createIdentifier,
createIfStatement,
createIndexSignatureDeclaration,
createMissingDeclaration,
createNodeArray,
createNumericLiteral,
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion tools/scripts/tsc/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -2497,7 +2497,7 @@
{
"name": "Type",
"inherited": true,
"optional": false
"optional": true
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions tsc/internal/checker/grammarchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/diagnostics/diagnosticMessages.generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down
8 changes: 4 additions & 4 deletions tsc/internal/diagnostics/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tsc/internal/diagnostics/diagnostics_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tsc/internal/diagnostics/loc/cs-CZ.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/de-DE.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/es-ES.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/fr-FR.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/it-IT.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/ja-JP.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/ko-KR.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/pl-PL.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/pt-BR.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/ru-RU.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/tr-TR.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/zh-CN.json.gz
Binary file not shown.
Binary file modified tsc/internal/diagnostics/loc/zh-TW.json.gz
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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.
}
Expand Down
Loading
Loading