# TypeScript中的typeof操作符

2 min read
Table of Contents

typeof 可以引用一个变量或属性的类型。

从基础类型取得类型

typeof 可以从基础类型取得类型,虽然作用不是很大。

let s = 'hello'
let n = typeof s
// let n: string

从对象中获取类型

当我们已经声明过一个变量类型,其他变量需要相同的类型时,我们可以使用 typeof 操作符

const value = { name: '', age: 0 }
let data: typeof value
// let data = {
// name: string;
// age: number;
// }

从函数中获取类型

从函数中获取类型是非常有用的。

function f() {
return { x: 10, y: 3 }
}
type P = ReturnType<typeof f>
// type P = {
// x: number;
// y: number;
// }

下面是一个创建一个联合类型的例子

function addPerson(personName: string) {
return {
type: 'AddPerson',
payload: personName,
} as const
}
function removePerson(id: number) {
return {
type: 'RemovePerson',
payload: id,
} as const
}
// 我们创建一个联合类型
type Actions = ReturnType<typeof addPerson> | ReturnType<typeof removePerson>
// type Actions = {
// readonly type: "AddPerson";
// readonly payload: string;
// } | {
// readonly type: "RemovePerson";
// readonly payload: number;
// }

参考链接


More Posts