Skip to content

Commit a5ca492

Browse files
committed
Fix computed property names of types and abstract members
1 parent 1044c5c commit a5ca492

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ namespace ts {
18681868
if (
18691869
!(useSite.flags & NodeFlags.Ambient) &&
18701870
!isPartOfTypeQuery(useSite) &&
1871-
!isPartOfPossiblyValidComputedPropertyNameExpression(useSite) &&
1871+
!isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite) &&
18721872
isExpressionNode(useSite)
18731873
) {
18741874
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);

src/compiler/utilities.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1778,11 +1778,22 @@ namespace ts {
17781778
return node.kind === SyntaxKind.TypeQuery;
17791779
}
17801780

1781-
export function isPartOfPossiblyValidComputedPropertyNameExpression(node: Node) {
1781+
export function isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(node: Node) {
17821782
while (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression) {
17831783
node = node.parent;
17841784
}
1785-
return node.kind === SyntaxKind.ComputedPropertyName;
1785+
if (node.kind !== SyntaxKind.ComputedPropertyName) {
1786+
return false;
1787+
}
1788+
if (hasModifier(node.parent, ModifierFlags.Abstract)) {
1789+
return true;
1790+
}
1791+
const containerKind = node.parent.parent.kind;
1792+
return containerKind === SyntaxKind.InterfaceDeclaration || containerKind === SyntaxKind.TypeLiteral;
1793+
}
1794+
1795+
export function isAbstractDeclarationName(node: Node) {
1796+
return isDeclarationName(node) && hasModifier(node, ModifierFlags.Abstract);
17861797
}
17871798

17881799
export function isExternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration & { moduleReference: ExternalModuleReference } {

tests/cases/conformance/externalModules/typeOnly/computedPropertyName.ts

+32
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,35 @@ import type { onInit } from "./framework-hooks";
99
interface Component {
1010
[onInit]?(): void;
1111
}
12+
13+
type T = {
14+
[onInit]: any;
15+
}
16+
17+
const o = {
18+
[onInit]: 0 // Error
19+
};
20+
21+
class C {
22+
[onInit]: any; // Error (because class fields)
23+
}
24+
25+
class D {
26+
[onInit] = 0; // Error
27+
}
28+
29+
class E {
30+
[onInit]() {} // Error
31+
}
32+
33+
abstract class F {
34+
abstract [onInit](): void;
35+
}
36+
37+
class G {
38+
declare [onInit]: any;
39+
}
40+
41+
declare class H {
42+
[onInit]: any;
43+
}

0 commit comments

Comments
 (0)