microsoft / microsoft/TypeScript
Allow constant strings as string literals for dynamic import statements.
@rbuckton arbeitet bereits daran.
Seit 15.7.2019.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Search Terms
Use constant string in place of string literal for dynamic import statements.
Suggestion
Honestly I don’t know if this is a bug or a suggestion.
Is there a way to allow constant string to represent string literals? I’m thinking this should work, but it doesn’t. For example:
const moduleName = "../../../Project/src/Mod";
async function doSomething() {
var mod = await import(moduleName); // (works only if a string literal at the moment)
}
In the case above, "mod" is of type "any" because the compiler doesn't recognize the string literal in the constant moduleName (for literal strings, the types are correctly pulled). I'm not sure if this was an oversight, but it makes sense to allow it, since constant strings cannot be reassigned. The only workaround is to wrap await import("../../../Project/src/Mod"); in a function:
async function getMod() { return import("../../../Project/src/Mod"); }
async function doSomething() {
var mod = await getMod(); // (method required since const strings cannot be used to import module type)
}
I may also add, it seems very difficult to import namespaces using dynamic imports, which I think is another terrible oversight.
async function doSomething() {
var mod = await import("../../../Project/src/Mod"); // (forced to use a string literal to get typings)
var o: mod.SomeClass; // ERROR, cannot find namespace 'mod'
// var o: InstanceType<typeof mod.SomeClass>; // workaround1
// var o: import("../../../Project/src/Mod").SomeClass; // workaround2 (who wants to keep typing full paths? A const string would be nice here.)
}
That doesn't even make sense. A namespace, while a type, is still a reference under the hood, and thus should still be importable dynamically somehow; perhaps like:
async function doSomething() {
import mod = await import("../../../Project/src/Mod"); // (forced to use a string literal to get typings)
var o: mod.SomeClass;
}
I think all this would aim to better support dynamic imports "on demand" instead of modules forcibly loading every single module when some may not be needed at all, It could also help promote faster initial page loads in many cases. ;)
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.