diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0da2e31137..cbae09a25a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16099,15 +16099,22 @@ namespace ts { function checkImportCallExpression(node: ImportCall): Type { // Check grammar of dynamic import - if (checkGrammarArguments(node, node.arguments) || checkGrammarImportCallExpression(node)) { + checkGrammarArguments(node, node.arguments) || checkGrammarImportCallExpression(node); + + if (node.arguments.length === 0) { return createPromiseReturnType(node, anyType); } - const specifier = node.arguments[0]; - const specifierType = checkNonNullExpression(specifier); - if (!isTypeAssignableTo(specifierType, stringType)) { + const specifierType = checkExpressionCached(specifier); + // Even though multiple arugments is grammatically incorrect, type-check extra arguments for completion + for (let i = 1; i < node.arguments.length; ++i) { + checkExpressionCached(node.arguments[i]); + } + + if (specifierType.flags & TypeFlags.Undefined || specifierType.flags & TypeFlags.Null || !isTypeAssignableTo(specifierType, stringType)) { error(specifier, Diagnostics.Dynamic_import_s_specifier_must_be_of_type_string_but_here_has_type_0, typeToString(specifierType)); } + // resolveExternalModuleName will return undefined if the moduleReferenceExpression is not a string literal const moduleSymbol = resolveExternalModuleName(node, specifier); if (moduleSymbol) { diff --git a/tests/baselines/reference/importCallExpression5ES2018.errors.txt b/tests/baselines/reference/importCallExpression5ES2018.errors.txt index 372fda0a68..a783612418 100644 --- a/tests/baselines/reference/importCallExpression5ES2018.errors.txt +++ b/tests/baselines/reference/importCallExpression5ES2018.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/es2018/dynamicImport/2.ts(3,23): error TS2532: Object is possibly 'undefined'. -tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS2532: Object is possibly 'undefined'. -tests/cases/conformance/es2018/dynamicImport/2.ts(5,24): error TS2531: Object is possibly 'null'. -tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is possibly 'null'. +tests/cases/conformance/es2018/dynamicImport/2.ts(3,23): error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./0" | undefined'. +tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'. +tests/cases/conformance/es2018/dynamicImport/2.ts(5,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./1" | null'. +tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'. ==== tests/cases/conformance/es2018/dynamicImport/0.ts (0 errors) ==== @@ -19,13 +19,13 @@ tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is const specify = bar() ? "./0" : undefined; let myModule = import(specify); ~~~~~~~ -!!! error TS2532: Object is possibly 'undefined'. +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./0" | undefined'. let myModule1 = import(undefined); ~~~~~~~~~ -!!! error TS2532: Object is possibly 'undefined'. +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'. let myModule2 = import(bar() ? "./1" : null); ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2531: Object is possibly 'null'. +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type '"./1" | null'. let myModule3 = import(null); ~~~~ -!!! error TS2531: Object is possibly 'null'. \ No newline at end of file +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'. \ No newline at end of file diff --git a/tests/baselines/reference/importCallExpression6ES2018.errors.txt b/tests/baselines/reference/importCallExpression6ES2018.errors.txt index 33f93ef4d3..2e34940856 100644 --- a/tests/baselines/reference/importCallExpression6ES2018.errors.txt +++ b/tests/baselines/reference/importCallExpression6ES2018.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS2532: Object is possibly 'undefined'. -tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is possibly 'null'. +tests/cases/conformance/es2018/dynamicImport/2.ts(4,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'. +tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'. ==== tests/cases/conformance/es2018/dynamicImport/0.ts (0 errors) ==== @@ -18,8 +18,8 @@ tests/cases/conformance/es2018/dynamicImport/2.ts(6,24): error TS2531: Object is let myModule = import(specify); let myModule1 = import(undefined); ~~~~~~~~~ -!!! error TS2532: Object is possibly 'undefined'. +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'. let myModule2 = import(bar() ? "./1" : null); let myModule3 = import(null); ~~~~ -!!! error TS2531: Object is possibly 'null'. \ No newline at end of file +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'null'. \ No newline at end of file diff --git a/tests/baselines/reference/importCallExpressionGrammarError.errors.txt b/tests/baselines/reference/importCallExpressionGrammarError.errors.txt index c4c62b8ff8..c0684edaab 100644 --- a/tests/baselines/reference/importCallExpressionGrammarError.errors.txt +++ b/tests/baselines/reference/importCallExpressionGrammarError.errors.txt @@ -2,10 +2,12 @@ tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(7,17): error TS1325: Specifier of dynamic import cannot be spread element. tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(8,12): error TS1324: Dynamic import must have one specifier as an argument. tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(9,19): error TS1135: Argument expression expected. +tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(9,19): error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'. tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(10,12): error TS1324: Dynamic import must have one specifier as an argument. +tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts(10,19): error TS2307: Cannot find module 'pathToModule'. -==== tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts (5 errors) ==== +==== tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts (7 errors) ==== declare function getSpecifier(): string; declare var whatToLoad: boolean; @@ -23,6 +25,10 @@ tests/cases/conformance/es2018/dynamicImport/importCallExpressionGrammarError.ts const p3 = import(,); !!! error TS1135: Argument expression expected. + +!!! error TS7036: Dynamic import's specifier must be of type 'string', but here has type 'undefined'. const p4 = import("pathToModule", "secondModule"); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1324: Dynamic import must have one specifier as an argument. \ No newline at end of file +!!! error TS1324: Dynamic import must have one specifier as an argument. + ~~~~~~~~~~~~~~ +!!! error TS2307: Cannot find module 'pathToModule'. \ No newline at end of file