typeod 操作符可以用来获取一个变量或对象的类型

    1. interface Person{
    2. name:string
    3. age:number
    4. }
    5. const sem:Person = {name:"ss",age:12}
    6. //获取sem变量的类型并赋值给Sem类型变量
    7. type Sem = typeof sem
    8. const lolo:Sem = {name:"lolo",age:5}

    也可以对嵌套对象执行相同操作

    1. const kakuqo = {
    2. name: "kakuqo",
    3. age: 30,
    4. address: {
    5. province: '福建',
    6. city: '厦门'
    7. }
    8. }
    9. type Kakuqo = typeof kakuqo

    此外,typeof 操作符除了可以获取对象的结构类型,也可以用来获函数对象的类型

    1. function toArrary(x:number):number[]{
    2. return [x]
    3. }
    4. type Func = typeof toArrary // (x:number)=>number[]