How to declare an object with nested array of objects in TypeScript?

后端 未结 2 549
陌清茗
陌清茗 2021-01-14 09:52

I have two classes like so.

class Stuff {
  constructor() { }
  things: Thing[] = [];
  name: string;
}

class Thing {
  constructor() { }
  active: boolean;         


        
2条回答
  •  执念已碎
    2021-01-14 10:16

    try to use <> or the as keyword for casting:

    blopp: Stuff[] = [
      {name: "aa", things: [{active: true} as Thing , {active: false}as Thing]}, 
      {name: "bb", things: null}];
    }
    

    or

    blopp: Stuff[] = [
      {name: "aa", things: [{active: true}  , {active: false}]}, 
      {name: "bb", things: null}];
    }
    

提交回复
热议问题