我想做2件事情:1:动态导入名称为2的包:声明包的类型(我知道它将返回什么)
type Task = [
Object,
Function
];
const getTasks: Promise<Task[]> = async (names: Array<string>) => {
const pkgs = names.map(name => import(name) as Promise<Task>);
return Promise.all(pkgs)
}
错误输出:
Type '(names: string[]) => Promise<[Object, Function][]>' is missing the following properties from type 'Promise<[Object, Function][]>': then, catch, [Symbol.toStringTag], finallyts(2739)
index.ts(55, 35): Did you mean to call this expression?
打字稿:3.3.3
Did you mean to call this expression?
错误消息中的,表明函数位于期望值的位置。getTasks
被声明为a,Promise
但右侧的表达式是一个函数。
根据意图的不同,有2个选项。要么立即调用该函数:
const getTasks: Promise<Task[]> = (async (names: Array<string>) => {
const pkgs = names.map(name => import(name) as Promise<Task>);
return Promise.all(pkgs)
})(['package-1', 'package-2']);
或者(如名称getTasks
所示)修复类型以反映它getTasks
是一个函数:
const getTasks: (names: Array<String>) => Promise<Task[]> = async (names: Array<string>) => {
const pkgs = names.map(name => import(name) as Promise<Task>);
return Promise.all(pkgs)
}
// or
const getTasks = async (names: Array<string>): Promise<Task[]> => {
const pkgs = names.map(name => import(name) as Promise<Task>);
return Promise.all(pkgs)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句