diff --git a/src/diagnosticMessages.json b/src/diagnosticMessages.json index e15946207a..3ff9e9f162 100644 --- a/src/diagnosticMessages.json +++ b/src/diagnosticMessages.json @@ -119,6 +119,7 @@ "Decorators are not valid here.": 1206, "'abstract' modifier can only appear on a class, method, or property declaration.": 1242, "Method '{0}' cannot have an implementation because it is marked abstract.": 1245, + "An interface property cannot have an initializer.": 1246, "A definite assignment assertion '!' is not permitted in this context.": 1255, "A class may only extend another class.": 1311, "A parameter property cannot be declared using a rest parameter.": 1317, diff --git a/src/program.ts b/src/program.ts index 1f9849eec2..c6bb4f7e37 100644 --- a/src/program.ts +++ b/src/program.ts @@ -2671,6 +2671,9 @@ export class Program extends DiagnosticEmitter { /** Parent interface. */ parent: InterfacePrototype ): void { + if (declaration.initializer) { + this.error(DiagnosticCode.An_interface_property_cannot_have_an_initializer, declaration.initializer.range); + } let typeNode = declaration.type; if (!typeNode) typeNode = Node.createOmittedType(declaration.name.range.atEnd); this.initializeProperty( diff --git a/tests/compiler/interface-with-initializer.json b/tests/compiler/interface-with-initializer.json new file mode 100644 index 0000000000..be54c01a3f --- /dev/null +++ b/tests/compiler/interface-with-initializer.json @@ -0,0 +1,7 @@ +{ + "asc_flags": [ + ], + "stderr": [ + "TS1246: An interface property cannot have an initializer." + ] +} diff --git a/tests/compiler/interface-with-initializer.ts b/tests/compiler/interface-with-initializer.ts new file mode 100644 index 0000000000..419366d43c --- /dev/null +++ b/tests/compiler/interface-with-initializer.ts @@ -0,0 +1,9 @@ +interface I { + v:string = "" +} + +class C implements I { + v:string = ""; +} + +new C();