我正在寻找一种方法来输入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')