📜  typescript returntype remove promise - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:25.938000             🧑  作者: Mango

代码示例1
type UnPromisifiedObject = {[k in keyof T]: UnPromisify}
type UnPromisify = T extends Promise ? U : T;

async function promise_props}>(obj: T): Promise> {
    const keys = Object.keys(obj);
    const awaitables = keys.map(key => obj[key]);

    const values = await Promise.all(awaitables);
    const result = {} as any;

    keys.forEach((key, i) => {
        result[key] = values[i];
    });
    return result as UnPromisifiedObject;
}

async function main() {
    const x = {
        company: Promise.resolve("company"),
        page: Promise.resolve(1)
    };
    const res = await promise_props(x);
    const company = res.company;  // company is a string here
    const page = res.page;        // page is a number here
}