Skip to content

Commit 124d746

Browse files
committed
Fix error on qualified names in type queries
1 parent 1cbfb81 commit 124d746

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed

src/compiler/checker.ts

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

18671867
function checkSymbolUsageInExpressionContext(symbol: Symbol, name: __String, useSite: Node) {
1868-
if (!(useSite.flags & NodeFlags.Ambient) && isInExpressionContext(useSite)) {
1868+
if (!(useSite.flags & NodeFlags.Ambient) && !isPartOfTypeQuery(useSite) && isExpressionNode(useSite)) {
18691869
const typeOnlyDeclaration = getTypeOnlyAliasDeclaration(symbol);
18701870
if (typeOnlyDeclaration) {
18711871
const message = typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier

src/compiler/utilities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,13 @@ namespace ts {
17711771
}
17721772
}
17731773

1774+
export function isPartOfTypeQuery(node: Node) {
1775+
while (node.kind === SyntaxKind.QualifiedName || node.kind === SyntaxKind.Identifier) {
1776+
node = node.parent;
1777+
}
1778+
return node.kind === SyntaxKind.TypeQuery;
1779+
}
1780+
17741781
export function isExternalModuleImportEqualsDeclaration(node: Node): node is ImportEqualsDeclaration & { moduleReference: ExternalModuleReference } {
17751782
return node.kind === SyntaxKind.ImportEqualsDeclaration && (<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference;
17761783
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/b.ts(2,21): error TS2339: Property 'A' does not exist on type 'typeof import("/a")'.
2+
3+
4+
==== /a.ts (0 errors) ====
5+
class A {}
6+
export type { A };
7+
export class B {};
8+
9+
==== /b.ts (1 errors) ====
10+
import * as types from './a';
11+
let A: typeof types.A;
12+
~
13+
!!! error TS2339: Property 'A' does not exist on type 'typeof import("/a")'.
14+
let B: typeof types.B;
15+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/conformance/externalModules/typeOnly/namespaceImportTypeQuery.ts] ////
2+
3+
//// [a.ts]
4+
class A {}
5+
export type { A };
6+
export class B {};
7+
8+
//// [b.ts]
9+
import * as types from './a';
10+
let A: typeof types.A;
11+
let B: typeof types.B;
12+
13+
14+
//// [a.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
var A = /** @class */ (function () {
18+
function A() {
19+
}
20+
return A;
21+
}());
22+
var B = /** @class */ (function () {
23+
function B() {
24+
}
25+
return B;
26+
}());
27+
exports.B = B;
28+
;
29+
//// [b.js]
30+
"use strict";
31+
exports.__esModule = true;
32+
var A;
33+
var B;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== /a.ts ===
2+
class A {}
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
export type { A };
6+
>A : Symbol(A, Decl(a.ts, 1, 13))
7+
8+
export class B {};
9+
>B : Symbol(B, Decl(a.ts, 1, 18))
10+
11+
=== /b.ts ===
12+
import * as types from './a';
13+
>types : Symbol(types, Decl(b.ts, 0, 6))
14+
15+
let A: typeof types.A;
16+
>A : Symbol(A, Decl(b.ts, 1, 3))
17+
>types : Symbol(types, Decl(b.ts, 0, 6))
18+
19+
let B: typeof types.B;
20+
>B : Symbol(B, Decl(b.ts, 2, 3))
21+
>types.B : Symbol(types.B, Decl(a.ts, 1, 18))
22+
>types : Symbol(types, Decl(b.ts, 0, 6))
23+
>B : Symbol(types.B, Decl(a.ts, 1, 18))
24+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== /a.ts ===
2+
class A {}
3+
>A : A
4+
5+
export type { A };
6+
>A : A
7+
8+
export class B {};
9+
>B : B
10+
11+
=== /b.ts ===
12+
import * as types from './a';
13+
>types : typeof types
14+
15+
let A: typeof types.A;
16+
>A : any
17+
>types.A : any
18+
>types : typeof types
19+
>A : any
20+
21+
let B: typeof types.B;
22+
>B : typeof types.B
23+
>types.B : typeof types.B
24+
>types : typeof types
25+
>B : typeof types.B
26+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @Filename: /a.ts
2+
class A {}
3+
export type { A };
4+
export class B {};
5+
6+
// @Filename: /b.ts
7+
import * as types from './a';
8+
let A: typeof types.A;
9+
let B: typeof types.B;

0 commit comments

Comments
 (0)