我无法理解为什么我的模型不会抛出错误而是自由地将我从httpClient调用中获得的日期字符串分配给模型中的Date类型。
这是代码
// Data model
export class Data {
purpose: string = null;
percentSold: number = null;
originalTerm: number = null;
originatedBy: string = null;
noteDate: Date = null;
productType: string = null;
maturityDate: Date = null;
noteAmount: number = null;
disburseDate: Date = null;
interestRate: number = null;
number: number = null;
branch: string = null;
principal: number = null;
amountAdvanced: number = null;
code: Code;
mailingAddress: AccountAddress;
constructor() {
this.code = new Code();
this.mailingAddress = new AccountAddress();
}
}
let data = new Data();
Object.assign(data, someObjectFromRouteCall);
// data object from Data model has noteDate and disimburseDate still be of type string

答案 0 :(得分:2)
分配在TypeScript未处于活动状态的运行时发生。它仅在编译期间进行类型检查。
您有两种选择:
Date
个对象,而是处理日期字符串。