1.Parameters
在typeScript中用来获取函数的参数类型关键词
语法:
Parameters<typeof 函数名称>
示例:
const test = (a: string, b: number, c: boolean) => {
return {
a,
b,
c,
};
};
type testType = Parameters<typeof test>;
// type testType1 = [a: string, b: number, c: boolean]
type testType1 = Parameters<typeof test>[0];
// type testType = string
type testType2 = Parameters<typeof test>[1];
// type testType2 = number
type testType3 = Parameters<typeof test>[2];
// type testType3 = boolean
2.ReturnType
在typeScript中用来获取函数的返回值类型关键词
语法:
ReturnType<typeof 函数>
示例:
返回值为person,则类型为personType
interface personType {
name: string;
age: number;
}
let person: personType = {
name: "John",
age: 30,
};
const personTest = (person: personType) => {
return person;
};
type personTestType = ReturnType<typeof personTest>;
// type personTestType = personType
返回值为person.name,则类型为string
interface personType {
name: string;
age: number;
}
let person: personType = {
name: "John",
age: 30,
};
const personTest = (person: personType) => {
return person.name;
};
type personTestType = ReturnType<typeof personTest>;
// type personTestType = string
返回值为person.age,则类型为number
interface personType {
name: string;
age: number;
}
let person: personType = {
name: "John",
age: 30,
};
const personTest = (person: personType) => {
return person.age;
};
type personTestType = ReturnType<typeof personTest>;
// type personTestType = number