How do I initialize a TypeScript object with a JSON object

前端 未结 16 981
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 08:30

I receive a JSON object from an AJAX call to a REST server. This object has property names that match my TypeScript class (this is a follow-on to this question).

Wha

16条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 09:17

    you can do like below

    export interface Instance {
      id?:string;
      name?:string;
      type:string;
    }
    

    and

    var instance: Instance = ({
          id: null,
          name: '',
          type: ''
        });
    

提交回复
热议问题