From 871d6095efaf1d90e98a14f6cb1fc4223ad16911 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 21 Mar 2017 15:08:00 -0700 Subject: [PATCH] Report an error if dynamic import specifier is not assigable to type string --- src/compiler/checker.ts | 4 ++++ src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 51593839104..3b9b9166589 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14873,6 +14873,10 @@ namespace ts { grammarErrorOnNode(node, Diagnostics.Dynamic_import_cannot_be_used_when_targeting_ECMAScript_2015_modules); } + const specifierType = checkExpression(node.specifier); + if (!isTypeAssignableTo(specifierType, stringType)) { + error(node.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, node.specifier); if (moduleSymbol) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 8e1ee767c55..5e200cf4a93 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3165,6 +3165,10 @@ "category": "Error", "code": 7035 }, + "Dynamic import's specifier must be of type 'string', but here has type '{0}'.": { + "category": "Error", + "code": 7036 + }, "You cannot rename this element.": { "category": "Error", "code": 8000