在TypeScript中,typeof操作符可以用来获取一个变量或对象的类型。
复制代码
interface Person {
name: string;
age: number;
}
const sem: Person = { name: "semlinker", age: 30}
type Sem = typeof sem; // type Sem = Person
复制代码
在上面代码中,我们通过typeof操作符获取sem变量的类型并赋值给Sem类型变量,之后我们就可以使用Sem类型:
const lolo: Sem = { name: "lolo", age: 5 }
你也可以对嵌套对象执行相同的操作:
复制代码
const kakuqo = {
name: "kakuqo",
age: 30,
address: {
province: '福建',
city: '厦门'
}
}
type Kakuqo = typeof kakuqo;
/**
- type Kakuqo = {
- name: string;
- age: number;
- address: {
- province: string;
- city: string;
- }
- }
*/
复制代码
此外,typeof操作符除了可以获取对象的结构类型之外,它也可以用来获取函数对象的类型,比如:
function toArray(x: number): Array
return [x]
}
type Func = typeof toArray; // -> (x: number) => number[]