动态导入的访问类型、路径和值



我正在寻找一种方法来输入example的参数,目前这段代码只是将其设置为any。操场上

function clientImport <T extends string>(v: T) {
const resolve = () => import(v)
type ResolveReturn = Awaited<ReturnType<typeof resolve>>
const example = (value: ResolveReturn) => {
return value
}
return {
path: v,
resolve,
example
}
}
const example = async () => {
const { path, resolve, example } = clientImport('./client_code.tsx')
console.log(path)
const v = await resolve()
//    ^?
example(v)
//  ^?
}

我想输入这个:

function clientImport <T extends string>(v: T) {
return function <Z>(x: (q: T) => Promise<Z>) {
return {
resolve: () => x(v),
path: v
}
}
}

const v = await clientImport('../examples/client_code/example.ts')(p => import(p))

使用构建步骤(例如prism),您可以创建一个自动生成的函数:

function _clientImport <G>(
path: string,
code: () => Promise<G>
) {
return { path, code }
}
// this would be auto-generated
const library = {
'../examples/client_code/example.ts': _clientImport(
import.meta.resolve('../examples/client_code/example.ts'),
() => import('../examples/client_code/example.ts')
)
}
function clientImport (p: keyof typeof library) {
return library[p]
}
const g = clientImport('../examples/client_code/example.ts')

相关内容

  • 没有找到相关文章

最新更新