TypeScript 学习笔记 - infer
type FlattenIfArray<T> = T extends Array<infer R> ? R : T;type FlattenIfArray<T> = T extends (infer R)[] ? R : T;type Unpromisify<T> = T extends Promise<infer R> ? R : T;type FunctionWithOneObjectArgument<P extends { [x: string]: any }, R> = (
props: P,
) => R;
type DestructuredArgsOfFunction<
F extends FunctionWithOneObjectArgument<any, any>
> = F extends FunctionWithOneObjectArgument<infer P, any> ? P : never;
const myFunction = (props: { x: number; y: number }): string => {
return 'ok';
};
const props: DestructuredArgsOfFunction<typeof myFunction> = {
x: 1,
y: 2,
};Last updated