【发布时间】:2016-03-14 04:34:34
【问题描述】:
我有一个带有私有属性的类,但是当我尝试使用这些元素创建一个数组时,我收到以下错误:
error TS2322: Type '{ "id": number; "totalTime": number; "team": { "id": number; "name": string; }; }[]' is not assignable to type 'Training[]'.
[0] Type '{ "id": number; "totalTime": number; "team": { "id": number; "name": string; }; }' is not assignable to type 'Training'.
[0] Property 'remainingTime' is missing in type '{ "id": number; "totalTime": number; "team": { "id": number; "name": string; }; }'
数组类:
import { TEAMS } from './mock-teams';
import { Training } from './training';
export var TRAININGS: Training[] = [
{"id": 0, "totalTime": 60, "team": {"id": TEAMS[0]['id'], "name": TEAMS[0]['name'] } },
{"id": 1, "totalTime": 60, "team": {"id": TEAMS[0]['id'], "name": TEAMS[0]['name'] } }
];
培训班:
export class Training {
private remainingTime: number = 0;
constructor(
public id: number,
public totalTime: number,
public team?: Team) {
}
}
我不明白为什么打字稿会抱怨甚至不在构造函数中的私有属性。
【问题讨论】:
标签: typescript angular