-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow constant strings as string literals for dynamic import statements. #32401
Comments
@rbuckton thoughts? |
Wouldn't this possibly involve needing to parse and rebind during check if the imported file wasn't previously part of the compilation? I could only see this work for files already included in the program. |
@RyanCavanaugh: Should we consider this for files included in the program? Would we need a distinct error message for files not included in the program (possibly only related to an implicit Usually the way I'd work around this today would be something like: const id = 'foo';
const foo = (await import(id)) as typeof import('foo'); The only other approach I could see would be an extra step in |
You'd need to duplicate the whole logic for scopes, shadowing and symbol lookup. That becomes even more difficult if the import in within a namespace because that changes the lookup completely. |
Precisely, it wouldn't be 100% reliable. |
@rbuckton I tried that also, but imagine having to do that 20 times in many files. It would be best to have a shared module, or global file, with constants to module paths. Using your example you would still have "../../../../very/long/paths" in 2 places (and scattered around code everywhere) - all of which must be updated when files move around, which is bad. A better work-around might be:
Unfortunate extra work, but at least the path is only ever in 2 places in the whole app. I might add, I'm trying to create an API for people to use, and I will be auto generating modules dynamically. So far, my best option is this:
Then the end user in their code only has to do this:
Except I hit another wall where "module" is "not a valid namespace" for types:
(which I admit is another issue) Yes, there are workarounds (using
That is actually my biggest issue right now. |
Just to throw my use case in as well as i'd actually really like generics in import types.
Where all my API endpoints export a default endpoint and which returns some data. |
Seeing the new import stuff land in TypeScript 5.3 is awesome, I quickly scrolled through the change notes to see if const strings were usable in import statements, alas no such feature landed. Would love to be able to use this as part of an RPC project that imports libraries as Workers. Currently it's something to the effect of: function importWorker<T, F extends keyof T>(url: URL, workFunction: F) -> T[F]{ ... }
importWorker<typeof import('./worker.js'), 'worker'>(new URL('./worker.js'), 'worker') It would be great to be able to use the following instead: importWorker(new URL('./worker.js'), 'worker') |
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:
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 wrapawait import("../../../Project/src/Mod");
in a function:I may also add, it seems very difficult to import namespaces using dynamic imports, which I think is another terrible oversight.
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:
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:
The text was updated successfully, but these errors were encountered: