Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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中实施必填字段(并使用var arg构造函数)?_Typescript - Fatal编程技术网

在TypeScript中实施必填字段(并使用var arg构造函数)?

在TypeScript中实施必填字段(并使用var arg构造函数)?,typescript,Typescript,由于TypeScript 1.3还没有抽象字段,因此要求设置字段的方法之一是将其作为构造函数参数 但是,由于此版本也没有扩展运算符,如果子级还必须将var arg传递给父级,则此操作不太有效。例如: class Parent { constructor (required1, required2, ...rest) { //... } class Child extends Parent { constructor (...args) { super(val1,

由于TypeScript 1.3还没有抽象字段,因此要求设置字段的方法之一是将其作为构造函数参数

但是,由于此版本也没有扩展运算符,如果子级还必须将var arg传递给父级,则此操作不太有效。例如:

class Parent {
    constructor (required1, required2, ...rest) { //...
}

class Child extends Parent {
    constructor (...args) {
       super(val1, val2, ???);
}
在普通的旧JavaScript中,此模式通常是通过切片并将
参数应用于超级构造函数来实现的。但是,这种变通方法不起作用,因为只能调用
super
,而不能启用
apply()


我错过什么了吗?否则,还有其他方法可以实现同样的效果吗?

遗憾的是,您需要欺骗TypeScript编译器

class Parent {
    constructor (required1, required2, ...rest) { //...
        if(required1 == void 0) return;
    }
}

class Child extends Parent {
    constructor (...args) {
       super(undefined,undefined); // make the compiler happy
       Parent.apply(this,[val1,val2].concat(args)) // the actual call  
    }
}
我已经记录了一个建议,以获得TS团队的意见: