Typescript “类”;实施;另一个类--获取错误。寻求澄清
有人能解释一下为什么不正确吗 又来了Typescript “类”;实施;另一个类--获取错误。寻求澄清,typescript,Typescript,有人能解释一下为什么不正确吗 又来了 // This is defined in a d.ts file. class Test { someObj: { someString: 'this'|'that'|'the other' }; } // This is me actually using the class. class Test2 implements Test { someObj = { someString: 'this
// This is defined in a d.ts file.
class Test {
someObj: {
someString: 'this'|'that'|'the other'
};
}
// This is me actually using the class.
class Test2 implements Test {
someObj = {
someString: 'this'
}
}
提前感谢:)这似乎是一个bug,因为即使使用
扩展
而不是实现
,它也不起作用,但这很好:
class Test {
someObj: {
someString: 'this'|'that'|'the other'
};
}
class Test2 extends Test {
constructor() {
super();
this.someObj = {
someString: 'this'
}
}
}
并且都编译成相同的js:
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Test = (function () {
function Test() {
}
return Test;
}());
var Test2 = (function (_super) {
__extends(Test2, _super);
function Test2() {
_super.call(this);
this.someObj = {
someString: 'this'
};
}
return Test2;
}(Test));
(除了代码中的:\u super.apply(这个参数)
)
你可以在上发布一个问题,如果你这样做,请将url作为评论发布,因为我想继续发布
如果您确实打算使用实现
而不是扩展
,那么使用构造函数并不能解决您的问题,但这样做会:
type SomeObj = {
someString: 'this'|'that'|'the other'
};
class Test {
someObj: SomeObj;
}
class Test2 implements Test {
someObj: SomeObj = {
someString: 'this'
}
}
感谢您进一步了解尼赞。问题是: