TypeScript
Parameters
- 官网链接
- 获取函数的参数类型
// Parameters<Type>
const foo = (arg1: string, arg2: number): void => {};
type FooType = Parameters<typeof foo>;
type FooFirstType = Parameters<typeof foo>[0];
const fun = (...args: FooType) => {
console.log(args);
};
const funFirst = (arg: FooFirstType) => {
console.log(arg);
};
fun('1', 2);
funFirst('1');
ReturnType
- 官网链接
- 获取函数的返回值类型
// ReturnType<Type>
const foo = (a: number, b: string) => {
return { a, b };
};
type Foo = ReturnType<typeof foo>;
const a: ReturnType<() => string> = 'string';
const b: ReturnType<() => number[]> = [1, 2, 3];
const params: Foo = { a: 1, b: 'b' };