Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在typescript中定义对象键的类型_Typescript_Class_Interface_Typescript Typings - Fatal编程技术网

在typescript中定义对象键的类型

在typescript中定义对象键的类型,typescript,class,interface,typescript-typings,Typescript,Class,Interface,Typescript Typings,我是typescript新手,我想为我的对象键定义类型,我已经检查了几种实现方法,但当分配不同类型的值时不会抛出错误。e、 g interface sectionProp { _type1: String, _columnStrechAllowed: Boolean, _columnOccupancy: Number, is_mandatory: Boolean } export class sectionProperties { folioSectio

我是typescript新手,我想为我的对象键定义类型,我已经检查了几种实现方法,但当分配不同类型的值时不会抛出错误。e、 g

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)
接口部分属性{
_类型1:字符串,
_columnStrechAllowed:布尔值,
_入住人数:,
_是必填项:布尔值
}
导出类sectionProperties{
folioSectionContentProp={}
构造函数(类型?){
this.folioSectionContentProp.\u type1=type;
this.folioSectionContentProp.\u columnStrechAllowed=false;
这个.folioSectionContentProp.\u column占用率=6;
this.folioSectionContentProp.is_必填=false;
}
}
导出类createNewSection扩展了sectionProperties{
构造函数(){
super(“Test”)//这里我将赋值
//super(12)/@这也不会产生任何错误
//我可以通过其他方式赋值(从对象创建)
//但我想知道,如果定义了类型,那么它应该
//不接受类型以外的任何值
}
}
var z=新的createNewSection();
console.log(z)
PS:我想定义我的对象键类型


谢谢

问题是您实际上没有在
sectionProperties
的构造函数中为
type
参数指定值,因此假定它是
any
,并且any可以从任何对象分配给任何对象。您需要显式类型批注:

interface sectionProp {
    _type1: String,
    _columnStrechAllowed: Boolean,
    _columnOccupancy: Number,
    is_mandatory: Boolean
}


export class sectionProperties {
    folioSectionContentProp = <sectionProp>{}

    constructor(type: string) {
        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)     //would be an error
    }

}
接口部分属性{
_类型1:字符串,
_columnStrechAllowed:布尔值,
_入住人数:,
_是必填项:布尔值
}
导出类sectionProperties{
folioSectionContentProp={}
构造函数(类型:字符串){
this.folioSectionContentProp.\u type1=type;
this.folioSectionContentProp.\u columnStrechAllowed=false;
这个.folioSectionContentProp.\u column占用率=6;
this.folioSectionContentProp.is_必填=false;
}
}
导出类createNewSection扩展了sectionProperties{
构造函数(){
super(“Test”)//这里我将赋值
//super(12)//将是一个错误
}
}

您可以考虑指定<代码> NoPyTyNyUn>代码>编译器选项强制您指定编译器无法推断的类型。

谢谢,如果我们在构造函数中显式地定义了比需要接口的类型(我们在哪里定义键的类型)?我希望我没有把两者混为一谈。因为如果我们为sectionProperties构造函数定义类型,则只会在那里处理该情况。@Mohd.moni不确定我是否明白您的意思。
sectionProp
中的
type1
字段是一个字符串。是否要在派生类型(如
createNewSection
)中使用从
sectionProp
派生的不同类型?如果我这样赋值
z.folioSectionContentProp.\u type1=12
;它会产生错误,这就是我所需要的。我将这两种方法与构造函数调用混合在一起,不检查类型,并将类型放入构造函数中进行检查,所以我想接口的用途是什么。我现在知道了。谢谢