I have function that works a bit like a wrapper for dynamic import()
syntax:
async importWrapper(url) {
return await import(url);
}
Is there a way I can make this function use a return type based on the value of the url
parameter?
I tried:
async function importWrapper<T extends string>(url: T) : Promise<typeof import(T)> {
return await import(url);
}
but this gives an error:
Promise<typeof import(T)>
// ^^^--- TS1141 String literal expected
CodePudding user response:
It is not possible, at least as of TypeScript 4.6, to express "import
types" of a form like import(T)
or typeof import(T)
.
There is an open feature request at microsoft/TypeScript#44636 asking for support for something like this. It is currently marked as "Awaiting Feedback" which means they would like to see what the community thinks before considering adopting such a feature. So you might want to give the issue a