我有三种类型:

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);

最后一行由于类型不兼容而抛出错误,它非常长,基本上说“dog缺少cat的属性”。
我要做的只是异步调用这3个函数并保存它们的结果。

最佳答案

我要做的只是异步调用这3个函数并保存它们的结果。
你有这样的安排:

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;
}

07-24 09:44
查看更多