Skip to content

Commit 4f15024

Browse files
authoredNov 15, 2023
fix: add diagnose when initializing field in interface (#2797)
1 parent aa54a2b commit 4f15024

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed
 

‎src/diagnosticMessages.json

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"Decorators are not valid here.": 1206,
120120
"'abstract' modifier can only appear on a class, method, or property declaration.": 1242,
121121
"Method '{0}' cannot have an implementation because it is marked abstract.": 1245,
122+
"An interface property cannot have an initializer.": 1246,
122123
"A definite assignment assertion '!' is not permitted in this context.": 1255,
123124
"A class may only extend another class.": 1311,
124125
"A parameter property cannot be declared using a rest parameter.": 1317,

‎src/program.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,10 @@ export class Program extends DiagnosticEmitter {
26712671
/** Parent interface. */
26722672
parent: InterfacePrototype
26732673
): void {
2674+
let initializer = declaration.initializer;
2675+
if (initializer) {
2676+
this.error(DiagnosticCode.An_interface_property_cannot_have_an_initializer, initializer.range);
2677+
}
26742678
let typeNode = declaration.type;
26752679
if (!typeNode) typeNode = Node.createOmittedType(declaration.name.range.atEnd);
26762680
this.initializeProperty(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"asc_flags": [
3+
],
4+
"stderr": [
5+
"TS1246: An interface property cannot have an initializer."
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
interface I {
2+
v: string = "";
3+
}
4+
5+
class C implements I {
6+
v: string = "";
7+
}
8+
9+
new C();

0 commit comments

Comments
 (0)
Please sign in to comment.