[TypeScript] “typeof” Type Queries

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
}

 

posted @ 2020-09-30 20:11  Zhentiw  阅读(131)  评论(0)    收藏  举报