【发布时间】:2019-01-02 09:11:19
【问题描述】:
我是打字稿的新手,我想为我的对象键定义类型,我已经检查了几种方法来实现它,但是在分配不同类型的值时不会抛出错误。例如
interface sectionProp {
_type1: String,
_columnStrechAllowed: Boolean,
_columnOccupancy: Number,
is_mandatory: Boolean
}
export class sectionProperties {
folioSectionContentProp = <sectionProp> {}
constructor(type?) {
this.folioSectionContentProp._type1 = type;
this.folioSectionContentProp._columnStrechAllowed = false;
this.folioSectionContentProp._columnOccupancy = 6;
this.folioSectionContentProp.is_mandatory = false;
}
}
export class createNewSection extends sectionProperties {
constructor() {
super("Test") // here I will assign value
// super(12) //@ this does not generate any error as well
// I might assign value by some other way (from object creation)
// but I want to know the reason if type is defined then it should
// not accept any value other than type
}
}
var z = new createNewSection();
console.log(z)
PS:我想定义我的对象键类型
谢谢
【问题讨论】:
标签: typescript class interface typescript-typings