Skip to content

Commit 1044c5c

Browse files
committed
Allow type-only imports in computed property names
1 parent bbbcbd5 commit 1044c5c

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,12 @@ namespace ts {
18651865
}
18661866

18671867
function checkSymbolUsageInExpressionContext(symbol: Symbol, name: __String, useSite: Node) {
1868-
if (!(useSite.flags & NodeFlags.Ambient) && !isPartOfTypeQuery(useSite) && isExpressionNode(useSite)) {
1868+
if (
1869+
!(useSite.flags & NodeFlags.Ambient) &&
1870+
!isPartOfTypeQuery(useSite) &&
1871+
!isPartOfPossiblyValidComputedPropertyNameExpression(useSite) &&
1872+
isExpressionNode(useSite)
1873+
) {
18691874
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);
18701875
if (typeOnlyDeclaration) {
18711876
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier

src/compiler/utilities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,13 @@ namespace ts {
17781778
return node.kind === SyntaxKind.TypeQuery;
17791779
}
17801780

1781+
export function isPartOfPossiblyValidComputedPropertyNameExpression(node: Node) {
1782+
while (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression) {
1783+
node = node.parent;
1784+
}
1785+
return node.kind === SyntaxKind.ComputedPropertyName;
1786+
}
1787+
17811788
export function isExternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration & { moduleReference: ExternalModuleReference } {
17821789
return node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference;
17831790
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/conformance/externalModules/typeOnly/computedPropertyName.ts] ////
2+
3+
//// [framework-hooks.ts]
4+
export const onInit = Symbol("onInit");
5+
6+
//// [component.ts]
7+
import type { onInit } from "./framework-hooks";
8+
9+
interface Component {
10+
[onInit]?(): void;
11+
}
12+
13+
14+
//// [framework-hooks.js]
15+
export const onInit = Symbol("onInit");
16+
//// [component.js]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/framework-hooks.ts ===
2+
export const onInit = Symbol("onInit");
3+
>onInit : Symbol(onInit, Decl(framework-hooks.ts, 0, 12))
4+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
5+
6+
=== tests/cases/conformance/externalModules/typeOnly/component.ts ===
7+
import type { onInit } from "./framework-hooks";
8+
>onInit : Symbol(onInit, Decl(component.ts, 0, 13))
9+
10+
interface Component {
11+
>Component : Symbol(Component, Decl(component.ts, 0, 48))
12+
13+
[onInit]?(): void;
14+
>[onInit] : Symbol(Component[onInit], Decl(component.ts, 2, 21))
15+
>onInit : Symbol(onInit, Decl(component.ts, 0, 13))
16+
}
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/externalModules/typeOnly/framework-hooks.ts ===
2+
export const onInit = Symbol("onInit");
3+
>onInit : unique symbol
4+
>Symbol("onInit") : unique symbol
5+
>Symbol : SymbolConstructor
6+
>"onInit" : "onInit"
7+
8+
=== tests/cases/conformance/externalModules/typeOnly/component.ts ===
9+
import type { onInit } from "./framework-hooks";
10+
>onInit : any
11+
12+
interface Component {
13+
[onInit]?(): void;
14+
>[onInit] : () => void
15+
>onInit : unique symbol
16+
}
17+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @target: esnext
2+
3+
// @Filename: framework-hooks.ts
4+
export const onInit = Symbol("onInit");
5+
6+
// @Filename: component.ts
7+
import type { onInit } from "./framework-hooks";
8+
9+
interface Component {
10+
[onInit]?(): void;
11+
}

0 commit comments

Comments
 (0)