我有3种类型:
export interface Animal {
...
}
export interface Cat extends Animal{
...
}
export interface Dog extends Animal{
...
}
export interface Iguana extends Animal {
...
}
在一个单独的文件中,我使用以下语法定义了3个异步函数:
const task1 = (() => Promise<Cat[]>) = async () => {
...
}
const task2 = (() => Promise<Dog[]>) = async () => {
...
}
const task3 = (() => Promise<Iguana[]>) = async () => {
...
}
const tasks = [task1, task2, task3];
const runningTasks = tasks.map(task => task());
// below line throws an error
const results = await Promise.all(runningTasks);
由于类型不兼容,最后一行引发错误,并且这非常长,并且基本上说“狗缺少Cat的属性”。
我想做的就是简单地异步调用这三个函数并保存它们的结果。
答案 0 :(得分:1)
我想做的就是简单地异步调用这三个函数并保存它们的结果。
您具有这种设置:
export interface Animal {
name: string;
}
export interface Cat extends Animal {
attitude: string;
}
export interface Dog extends Animal {
sleepDuration: number;
}
export interface Iguana extends Animal {
skinToughness: number;
}
const task1 = async (): Promise<Cat[]> => {
return await Promise.resolve([]);
}
const task2 = async (): Promise<Dog[]> => {
return await Promise.resolve([]);
}
const task3 = async (): Promise<Iguana[]> => {
return await Promise.resolve([]);
}
一种方法是像这样使用Promise
中的父类型:
const demo = async () => {
const tasks = [task1, task2, task3];
const runningTasks: Promise<Animal[]>[] = tasks.map(task => task());
const results: Animal[][] = await Promise.all(runningTasks);
}
如果从结果中使用特定类型很重要,则jcalz建议的元组将起作用:
const demo = async () => {
const tasks = [task1, task2, task3] as const;
type TaskResults = [
ReturnType<typeof task1>,
ReturnType<typeof task2>,
ReturnType<typeof task3>,
];
const runningTasks = tasks.map(task => task()) as TaskResults;
const results = await Promise.all(runningTasks);
results[0][0].attitude;
results[1][0].sleepDuration;
results[2][0].skinToughness;
}
令人惊讶的是,如果您不需要提前启动任务,则可以使用这种方法维护类型信息:
const demo = async () => {
const results = await Promise.all([
task1(),
task2(),
task3()
])
results[0][0].attitude;
results[1][0].sleepDuration;
results[2][0].skinToughness;
}