Skip to content

Commit 790c03d

Browse files
authored
fix(47954): Auto implementation of interface with a constructor prop causes error (microsoft#50709)
* fix(47954): convert constructor property to computed name * handle more nodes with constructor name
1 parent 8f2a38f commit 790c03d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/services/codefixes/helpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ export function addNewNodeForMemberSymbol(
305305
}
306306

307307
function createName(node: PropertyName) {
308+
if (isIdentifier(node) && node.escapedText === "constructor") {
309+
return factory.createComputedPropertyName(factory.createStringLiteral(idText(node), quotePreference === QuotePreference.Single));
310+
}
308311
return getSynthesizedDeepClone(node, /*includeTrivia*/ false);
309312
}
310313

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface I {
4+
//// constructor: number;
5+
////}
6+
////class C implements I {}
7+
8+
verify.codeFix({
9+
description: "Implement interface 'I'",
10+
newFileContent:
11+
`interface I {
12+
constructor: number;
13+
}
14+
class C implements I {
15+
["constructor"]: number;
16+
}`,
17+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface I {
4+
//// constructor(): number;
5+
////}
6+
////class C implements I {}
7+
8+
verify.codeFix({
9+
description: "Implement interface 'I'",
10+
newFileContent:
11+
`interface I {
12+
constructor(): number;
13+
}
14+
class C implements I {
15+
["constructor"](): number {
16+
throw new Error("Method not implemented.");
17+
}
18+
}`,
19+
});

0 commit comments

Comments
 (0)