Skip to content

Commit

Permalink
fix: add diagnose when initializing field in interface
Browse files Browse the repository at this point in the history
Fixes: #2795
  • Loading branch information
HerrCai0907 committed Nov 14, 2023
1 parent f055bff commit 212a88a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/program.ts
Expand Up @@ -2671,6 +2671,10 @@ export class Program extends DiagnosticEmitter {
/** Parent interface. */
parent: InterfacePrototype
): void {
let initializer = declaration.initializer;
if (initializer) {
this.error(DiagnosticCode.An_interface_property_cannot_have_an_initializer, initializer.range);
}
let typeNode = declaration.type;
if (!typeNode) typeNode = Node.createOmittedType(declaration.name.range.atEnd);
this.initializeProperty(
Expand Down
7 changes: 7 additions & 0 deletions tests/compiler/interface-with-initializer.json
@@ -0,0 +1,7 @@
{
"asc_flags": [
],
"stderr": [
"TS1246: An interface property cannot have an initializer."
]
}
9 changes: 9 additions & 0 deletions tests/compiler/interface-with-initializer.ts
@@ -0,0 +1,9 @@
interface I {
v:string = ""
}

class C implements I {
v:string = "";
}

new C();

0 comments on commit 212a88a

Please sign in to comment.