Types in object destructuring

前端 未结 4 1274
你的背包
你的背包 2020-11-27 13:40

This

const { foo: IFoo[] } = bar;

and this

const { foo: Array } = bar;

will reasonably cause

4条回答
  •  借酒劲吻你
    2020-11-27 14:25

    I'm clearly a bit late to the party, but:

    interface User {
      name: string;
      age: number;
    }
    
    const obj: any = { name: 'Johnny', age: 25 };
    const { name, age }: User = obj;
    

    The types of properties name and age should be correctly inferred to string and number respectively.

提交回复
热议问题