zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[TypeScript] “typeof” Type Queries

typescript type Queries typeof
2023-09-14 09:00:47 时间

In Javascript, you know typeof:

typeof [] // object
typeof "" // string

 

In Typescript it is more smart:

const person = {
    name: "wan",
    age: 28
}

type Person = typeof person // {name: string, age: number}, in javascript it will show "object"

const anotherPerson: Person = {
   name: 'whatever'.
   age: 31
}