Skip to content

Instantly share code, notes, and snippets.

@lilywang711
Last active July 30, 2020 06:11
Show Gist options
  • Save lilywang711/8ba412ddd227fd0f089315696528d552 to your computer and use it in GitHub Desktop.
Save lilywang711/8ba412ddd227fd0f089315696528d552 to your computer and use it in GitHub Desktop.
反解 promise 类型
type PromiseType<T> = (args: any[]) => Promise<T>;
type UnPromisify<T> = T extends PromiseType<infer U> ? U : never;
// 或者这样一句话: type UnPromisify<T> = T extends (args: any[]) => Promise<infer U> ? U : never;
// 测试 UnPromisify<T>:
async function stringPromise() {
return "string promise";
}
async function numberPromise() {
return 1;
}
interface Person {
name: string;
age: number;
}
async function personPromise() {
return { name: "Wayou", age: 999 } as Person;
}
type extractStringPromise = UnPromisify<typeof stringPromise>; // string
type extractNumberPromise = UnPromisify<typeof numberPromise>; // number
type extractPersonPromise = UnPromisify<typeof personPromise>; // Person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment