Reputation: 75
I want to do 2 things: 1: Dynamically import package with the name 2: Assert the type of the package(I know what will it return)
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)
}
Error output:
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?
Typescript: 3.3.3
Upvotes: 3
Views: 9090
Reputation: 4224
Umm I guess a small typo? You probably meant to write this...
const getTasks = async (names: Array<string>): Promise<Task[]> => { ... }
Upvotes: 1
Reputation: 3506
Did you mean to call this expression?
in the error message is suggesting that a function is in a place where a value is expected. getTasks
is declared to be a Promise
but the expression on the right hand side is a function.
There are 2 options depending on what the intention was. Either immediately invoke the function:
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']);
or (like the name getTasks
suggests) fix the type to reflect that getTasks
is a function:
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)
}
Upvotes: 6