如何在TypeScript中为对象元素指定接口类型?

时间:2019-06-24 11:17:50

标签: angular typescript

我想像这样为我的对象指定一个接口:

interface Item {
    id: number;
    size: number;
}


var obj  = {
    name: "test",
    items:Item = null 
}

并且我想在ngOninit()上从服务器中获取一些数据后为obj.items分配一些值。

有可能吗?

2 个答案:

答案 0 :(得分:1)

类型的构造

interface Item {
    id: number;
    size: number;
}

interface ParentItem {
    name: string;
    item: Item | null;
}

对象的构造:

let object: ParentItem = { name: "test", item: null };

答案 1 :(得分:0)

export interface ClientEmployee {
clientTeamID: number;
employeeID: number;
sharingPercent: number;
}
import 
 ClientEmployees = {
id: 0,
clientTeamID: 0,
employeeID: 0,
sharingPercent: 0,
clientName: ''
  };
 get in oninit 

 this.restService.GetTeamEmployees('').subscribe(response => {
  if (response.error == null && response.message === 'Success' && 
 response.data.length > 0) {
    this.ClientEmployees = response.data;
    this.loaderService.hide();
    }  });