Skip to content

Commit fc5c765

Browse files
authored
fix(44868): handle import type in shorthand assignment (#44881)
1 parent f58a662 commit fc5c765

16 files changed

+59
-4
lines changed

src/compiler/utilities.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7036,13 +7036,17 @@ namespace ts {
70367036
|| isPartOfTypeQuery(useSite)
70377037
|| isIdentifierInNonEmittingHeritageClause(useSite)
70387038
|| isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite)
7039-
|| !isExpressionNode(useSite);
7039+
|| !(isExpressionNode(useSite) || isShorthandPropertyNameUseSite(useSite));
70407040
}
70417041

70427042
export function typeOnlyDeclarationIsExport(typeOnlyDeclaration: Node) {
70437043
return typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier;
70447044
}
70457045

7046+
function isShorthandPropertyNameUseSite(useSite: Node) {
7047+
return isIdentifier(useSite) && isShorthandPropertyAssignment(useSite.parent) && useSite.parent.name === useSite;
7048+
}
7049+
70467050
function isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(node: Node) {
70477051
while (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.PropertyAccessExpression) {
70487052
node = node.parent;
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/b.ts(2,5): error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
2+
/b.ts(4,11): error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
23

34

45
==== /a.ts (0 errors) ====
56
export default class A { a!: string }
67

7-
==== /b.ts (1 errors) ====
8+
==== /b.ts (2 errors) ====
89
import type A from './a';
910
new A();
1011
~
1112
!!! error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
1213
!!! related TS1376 /b.ts:1:8: 'A' was imported here.
1314
let a: A = { a: '' };
15+
let b = { A };
16+
~
17+
!!! error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
18+
!!! related TS1376 /b.ts:1:8: 'A' was imported here.
1419

tests/baselines/reference/importClause_default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class A { a!: string }
77
import type A from './a';
88
new A();
99
let a: A = { a: '' };
10+
let b = { A };
1011

1112

1213
//// [a.js]
@@ -23,3 +24,4 @@ exports["default"] = A;
2324
exports.__esModule = true;
2425
new A();
2526
var a = { a: '' };
27+
var b = { A: A };

tests/baselines/reference/importClause_default.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ let a: A = { a: '' };
1515
>A : Symbol(A, Decl(b.ts, 0, 6))
1616
>a : Symbol(a, Decl(b.ts, 2, 12))
1717

18+
let b = { A };
19+
>b : Symbol(b, Decl(b.ts, 3, 3))
20+
>A : Symbol(A, Decl(b.ts, 3, 9))
21+

tests/baselines/reference/importClause_default.types

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ let a: A = { a: '' };
1717
>a : string
1818
>'' : ""
1919

20+
let b = { A };
21+
>b : { A: typeof A; }
22+
>{ A } : { A: typeof A; }
23+
>A : typeof A
24+
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/d.ts(2,5): error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
2+
/d.ts(6,13): error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
23

34

45
==== /abc.ts (0 errors) ====
56
export class A {}
67
export type B = { b: string };
78
export const C = "";
89

9-
==== /d.ts (1 errors) ====
10+
==== /d.ts (2 errors) ====
1011
import type { A, B, C } from './abc';
1112
new A();
1213
~
@@ -15,4 +16,8 @@
1516
declare let a: A;
1617
declare let b: B;
1718
b.b;
19+
const c = { A };
20+
~
21+
!!! error TS1361: 'A' cannot be used as a value because it was imported using 'import type'.
22+
!!! related TS1376 /d.ts:1:15: 'A' was imported here.
1823

tests/baselines/reference/importClause_namedImports.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ new A();
1111
declare let a: A;
1212
declare let b: B;
1313
b.b;
14+
const c = { A };
1415

1516

1617
//// [abc.js]
@@ -29,3 +30,4 @@ exports.C = "";
2930
exports.__esModule = true;
3031
new A();
3132
b.b;
33+
var c = { A: A };

tests/baselines/reference/importClause_namedImports.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ b.b;
3131
>b : Symbol(b, Decl(d.ts, 3, 11))
3232
>b : Symbol(b, Decl(abc.ts, 1, 18))
3333

34+
const c = { A };
35+
>c : Symbol(c, Decl(d.ts, 5, 5))
36+
>A : Symbol(A, Decl(d.ts, 5, 11))
37+

tests/baselines/reference/importClause_namedImports.types

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ b.b;
3131
>b : B
3232
>b : string
3333

34+
const c = { A };
35+
>c : { A: typeof A; }
36+
>{ A } : { A: typeof A; }
37+
>A : typeof A
38+

tests/baselines/reference/importClause_namespaceImport.errors.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/b.ts(4,14): error TS2694: Namespace '"/a"' has no exported member 'Value'.
44
/b.ts(5,7): error TS2741: Property 'a' is missing in type '{}' but required in type 'A'.
55
/b.ts(6,7): error TS2741: Property 'b' is missing in type '{}' but required in type 'B'.
6+
/b.ts(8,13): error TS1361: 'types' cannot be used as a value because it was imported using 'import type'.
67

78

89
==== /a.ts (0 errors) ====
@@ -11,7 +12,7 @@
1112
export type C<T> = T;
1213
export const Value = {};
1314

14-
==== /b.ts (5 errors) ====
15+
==== /b.ts (6 errors) ====
1516
import type * as types from './a';
1617
types;
1718
~~~~~
@@ -33,4 +34,8 @@
3334
!!! error TS2741: Property 'b' is missing in type '{}' but required in type 'B'.
3435
!!! related TS2728 /a.ts:2:18: 'b' is declared here.
3536
const c: types.C<string> = "";
37+
const d = { types };
38+
~~~~~
39+
!!! error TS1361: 'types' cannot be used as a value because it was imported using 'import type'.
40+
!!! related TS1376 /b.ts:1:13: 'types' was imported here.
3641

tests/baselines/reference/importClause_namespaceImport.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let v: types.Value;
1414
const a: types.A = {};
1515
const b: types.B = {};
1616
const c: types.C<string> = "";
17+
const d = { types };
1718

1819

1920
//// [a.js]
@@ -42,3 +43,4 @@ var v;
4243
var a = {};
4344
var b = {};
4445
var c = "";
46+
var d = { types: types };

tests/baselines/reference/importClause_namespaceImport.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ const c: types.C<string> = "";
4646
>types : Symbol(types, Decl(b.ts, 0, 11))
4747
>C : Symbol(types.C, Decl(a.ts, 1, 29))
4848

49+
const d = { types };
50+
>d : Symbol(d, Decl(b.ts, 7, 5))
51+
>types : Symbol(types, Decl(b.ts, 7, 11))
52+

tests/baselines/reference/importClause_namespaceImport.types

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ const c: types.C<string> = "";
4545
>types : any
4646
>"" : ""
4747

48+
const d = { types };
49+
>d : { types: typeof types; }
50+
>{ types } : { types: typeof types; }
51+
>types : typeof types
52+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export default class A { a!: string }
55
import type A from './a';
66
new A();
77
let a: A = { a: '' };
8+
let b = { A };

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ new A();
99
declare let a: A;
1010
declare let b: B;
1111
b.b;
12+
const c = { A };

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ let v: types.Value;
1212
const a: types.A = {};
1313
const b: types.B = {};
1414
const c: types.C<string> = "";
15+
const d = { types };

0 commit comments

Comments
 (0)