TypeScript RetourType Supprimer la promesse
type UnPromisifiedObject<T> = {[k in keyof T]: UnPromisify<T[k]>}
type UnPromisify<T> = T extends Promise<infer U> ? U : T;
async function promise_props<T extends {[key: string]: Promise<any>}>(obj: T): Promise<UnPromisifiedObject<T>> {
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<T>;
}
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
}
Prickly Porcupine