-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support string types in dynamic import types. #32705
Comments
Regular expression types would probably solve this nicely: #6579 |
Regular expression types would probably solve this nicely: #6579
<#6579>
I don't want to assert that the string looks like a path, I want to assert
that there is a module accessable at the path, and that the imports of the
module have a given shape.
…On Mon, Aug 5, 2019, 8:02 AM Bruce Pascoe ***@***.***> wrote:
Regular expression types would probably solve this nicely: #6579
<#6579>
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#32705?email_source=notifications&email_token=AAE6WM4CJWKYA7B2NWKEMY3QDA6HLA5CNFSM4IJGG3J2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3SC4MY#issuecomment-518270515>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAE6WM2B5WZ2DRTGBVVEKY3QDA6HLANCNFSM4IJGG3JQ>
.
|
Open to feedback here but we'd have to have some very strong and compelling use cases to investigate further. The process of constructing a "program" (the set of files under analysis) begins by following static constructs like |
I'll assume that dynamic import I can think of one way to implement this without much rejiggereing.
async function factory(path: string & Module<{default: Logger}>): Logger {
return import(path).then(module => module.default);
}
factory(import.later('./path-to-module')); This feels like a hack, so feel free to resolve this issue. IMO this pattern will become prevalent if people start authoring libraries that use web workers or worker threads, but I don't think there's much demand for the feature right now. |
Search Terms
dynamic import type
Suggestion
I'd like to define a function that wraps a dynamic
require
orimport
. Specifically, I'm writing a library that uses node's worker_thread and I want to support 'dependency injection' via dynamic import. You can't pass class instances or functions across execution contexts.I don't think you can currently define LoggerImportPath any more precisely than
string
andimport(path: string)
returnsPromise<any>
.Use Cases
I haven't seen this pattern in any other libraries, but I think it's a good way to pass classes or functions across threads or execution contexts. You can't currently limit the kinds of strings that library uses can pass when a module path is expected.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: