Skip to content

Commit 99ffa39

Browse files
authored
fix(48759): throw an error on using import expression with type arguments (microsoft#48787)
1 parent fd06132 commit 99ffa39

8 files changed

+43
-6
lines changed

src/compiler/checker.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -43901,6 +43901,9 @@ namespace ts {
4390143901
}
4390243902

4390343903
function checkGrammarExpressionWithTypeArguments(node: ExpressionWithTypeArguments | TypeQueryNode) {
43904+
if (isExpressionWithTypeArguments(node) && isImportKeyword(node.expression) && node.typeArguments) {
43905+
return grammarErrorOnNode(node, Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments);
43906+
}
4390443907
return checkGrammarTypeArguments(node, node.typeArguments);
4390543908
}
4390643909

@@ -44951,7 +44954,7 @@ namespace ts {
4495144954
}
4495244955

4495344956
if (node.typeArguments) {
44954-
return grammarErrorOnNode(node, Diagnostics.Dynamic_import_cannot_have_type_arguments);
44957+
return grammarErrorOnNode(node, Diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments);
4495544958
}
4495644959

4495744960
const nodeArguments = node.arguments;

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@
956956
"category": "Error",
957957
"code": 1325
958958
},
959-
"Dynamic import cannot have type arguments.": {
959+
"This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.": {
960960
"category": "Error",
961961
"code": 1326
962962
},
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/dynamicImport/1.ts(2,10): error TS1326: Dynamic import cannot have type arguments.
2-
tests/cases/conformance/dynamicImport/1.ts(3,10): error TS1326: Dynamic import cannot have type arguments.
1+
tests/cases/conformance/dynamicImport/1.ts(2,10): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
2+
tests/cases/conformance/dynamicImport/1.ts(3,10): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
33

44

55
==== tests/cases/conformance/dynamicImport/0.ts (0 errors) ====
@@ -9,7 +9,7 @@ tests/cases/conformance/dynamicImport/1.ts(3,10): error TS1326: Dynamic import c
99
"use strict"
1010
var p1 = import<Promise<any>>("./0"); // error
1111
~~~~~~~~~~~~~~~~~~~~~~~~~~~
12-
!!! error TS1326: Dynamic import cannot have type arguments.
12+
!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
1313
var p2 = import<>("./0"); // error
1414
~~~~~~~~~~~~~~~
15-
!!! error TS1326: Dynamic import cannot have type arguments.
15+
!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/types/import/importWithTypeArguments.ts(1,1): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
2+
tests/cases/conformance/types/import/importWithTypeArguments.ts(2,11): error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
3+
4+
5+
==== tests/cases/conformance/types/import/importWithTypeArguments.ts (2 errors) ====
6+
import<T>
7+
~~~~~~~~~
8+
!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
9+
const a = import<string, number>
10+
~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS1326: This use of 'import' is invalid. 'import()' calls can be written, but they must have parentheses and cannot have type arguments.
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [importWithTypeArguments.ts]
2+
import<T>
3+
const a = import<string, number>
4+
5+
6+
//// [importWithTypeArguments.js]
7+
import;
8+
var a = (import);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/types/import/importWithTypeArguments.ts ===
2+
import<T>
3+
>T : Symbol(T)
4+
5+
const a = import<string, number>
6+
>a : Symbol(a, Decl(importWithTypeArguments.ts, 1, 5))
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/conformance/types/import/importWithTypeArguments.ts ===
2+
import<T>
3+
const a = import<string, number>
4+
>a : any
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import<T>
2+
const a = import<string, number>

0 commit comments

Comments
 (0)