diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9ed3ca55e19..8ffa0782334 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16285,11 +16285,17 @@ namespace ts { if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { return true; } + if (node.initializer) { + return grammarErrorOnNode(node.initializer, Diagnostics.An_interface_property_cannot_have_an_initializer); + } } else if (node.parent.kind === SyntaxKind.TypeLiteral) { if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) { return true; } + if (node.initializer) { + return grammarErrorOnNode(node.initializer, Diagnostics.A_type_literal_property_cannot_have_an_initializer); + } } if (isInAmbientContext(node) && node.initializer) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 62661706373..0c19f6d8247 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -783,6 +783,14 @@ "category": "Error", "code": 1245 }, + "An interface property cannot have an initializer.": { + "category": "Error", + "code": 1246 + }, + "A type literal property cannot have an initializer.": { + "category": "Error", + "code": 1247 + }, "'with' statements are not allowed in an async function block.": { "category": "Error", @@ -2430,7 +2438,7 @@ "Not all code paths return a value.": { "category": "Error", "code": 7030 - }, + }, "You cannot rename this element.": { "category": "Error", "code": 8000 diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index fda1b1015d9..3a3350f2166 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2259,6 +2259,14 @@ namespace ts { property.name = name; property.questionToken = questionToken; property.type = parseTypeAnnotation(); + + if (token === SyntaxKind.EqualsToken) { + // Although type literal properties cannot not have initializers, we attempt + // to parse an initializer so we can report in the checker that an interface + // property or type literal property cannot have an initializer. + property.initializer = parseNonParameterInitializer(); + } + parseTypeMemberSemicolon(); return finishNode(property); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f8b26ddaa6d..5cd7c7d0bc3 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -587,6 +587,7 @@ namespace ts { name: PropertyName; // Declared property name questionToken?: Node; // Present on optional property type?: TypeNode; // Optional type annotation + initializer?: Expression; // Optional initializer } // @kind(SyntaxKind.PropertyDeclaration) diff --git a/tests/baselines/reference/errorOnInitializerInInterfaceProperty.errors.txt b/tests/baselines/reference/errorOnInitializerInInterfaceProperty.errors.txt new file mode 100644 index 00000000000..03e899e7c2d --- /dev/null +++ b/tests/baselines/reference/errorOnInitializerInInterfaceProperty.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts(2,19): error TS1246: An interface property cannot have an initializer. + + +==== tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts (1 errors) ==== + interface Foo { + bar: number = 5; + ~ +!!! error TS1246: An interface property cannot have an initializer. + } + \ No newline at end of file diff --git a/tests/baselines/reference/errorOnInitializerInInterfaceProperty.js b/tests/baselines/reference/errorOnInitializerInInterfaceProperty.js new file mode 100644 index 00000000000..c40a3cfb43d --- /dev/null +++ b/tests/baselines/reference/errorOnInitializerInInterfaceProperty.js @@ -0,0 +1,7 @@ +//// [errorOnInitializerInInterfaceProperty.ts] +interface Foo { + bar: number = 5; +} + + +//// [errorOnInitializerInInterfaceProperty.js] diff --git a/tests/baselines/reference/errorOnInitializerInObjectTypeLiteralProperty.errors.txt b/tests/baselines/reference/errorOnInitializerInObjectTypeLiteralProperty.errors.txt new file mode 100644 index 00000000000..f6d10b92b2d --- /dev/null +++ b/tests/baselines/reference/errorOnInitializerInObjectTypeLiteralProperty.errors.txt @@ -0,0 +1,17 @@ +tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts(2,19): error TS1247: A type literal property cannot have an initializer. +tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts(6,19): error TS1247: A type literal property cannot have an initializer. + + +==== tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts (2 errors) ==== + var Foo: { + bar: number = 5; + ~ +!!! error TS1247: A type literal property cannot have an initializer. + }; + + let Bar: { + bar: number = 5; + ~ +!!! error TS1247: A type literal property cannot have an initializer. + }; + \ No newline at end of file diff --git a/tests/baselines/reference/errorOnInitializerInObjectTypeLiteralProperty.js b/tests/baselines/reference/errorOnInitializerInObjectTypeLiteralProperty.js new file mode 100644 index 00000000000..4dcc0b7dce6 --- /dev/null +++ b/tests/baselines/reference/errorOnInitializerInObjectTypeLiteralProperty.js @@ -0,0 +1,13 @@ +//// [errorOnInitializerInObjectTypeLiteralProperty.ts] +var Foo: { + bar: number = 5; +}; + +let Bar: { + bar: number = 5; +}; + + +//// [errorOnInitializerInObjectTypeLiteralProperty.js] +var Foo; +var Bar; diff --git a/tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts b/tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts new file mode 100644 index 00000000000..ac600103e72 --- /dev/null +++ b/tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts @@ -0,0 +1,3 @@ +interface Foo { + bar: number = 5; +} diff --git a/tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts b/tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts new file mode 100644 index 00000000000..a02f8b0f709 --- /dev/null +++ b/tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts @@ -0,0 +1,7 @@ +var Foo: { + bar: number = 5; +}; + +let Bar: { + bar: number = 5; +};