[英]TYPESCRIPT : Argument of type 'typeof X' is not assignable to parameter of type 'Y' with extends
I am starting to code with typescript and I have a few compilation problems.我开始用打字稿编码,但我有一些编译问题。
I have several classes and it seems that I have a hierarchy problem我有几个班级,似乎我有层次结构问题
On my first class (A) I indicated a set of properties / functions.在我的第一堂课 (A) 上,我指出了一组属性/功能。 I have a second class (B) which inherits from class (A) and which adds certain properties / functions.
我有第二个类 (B),它继承自类 (A) 并添加了某些属性/功能。 Finally I have a third class (C) which inherits from the second class (B).
最后,我有一个继承自第二个类 (B) 的第三个类 (C)。
export default class A {
prop1: string;
function1() {
console.log('TEST');
}
}
export default class B extends A {
prop2: string;
function2() {
console.log('TEST');
}
}
export default class C extends B {
prop3: string;
function3() {
console.log('TEST');
}
}
When compiling, I get the following error message :编译时,我收到以下错误消息:
TS2345: Argument of type 'typeof C' is not assignable to parameter of type 'A'.
TS2345:“typeof C”类型的参数不可分配给“A”类型的参数。 Type 'typeof C' is missing the following properties from type 'B': prop1, function1.
类型“typeof C”缺少类型“B”中的以下属性:prop1、function1。
My 3 classes are in 3 separate files and I use export / import and it seems to work ...我的 3 个类在 3 个单独的文件中,我使用导出/导入,它似乎工作......
Do you have an idea?你有想法吗?
TSCONFING: TSCONFING:
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true
}
}
I'm trying to make a code like this link .我正在尝试制作类似此链接的代码。 The error messages that sound on the site are not quite the same as I have in my editor, but maybe when correcting on the site I will find the final problem ...
网站上的错误消息听起来与我在编辑器中的不太一样,但也许在网站上更正时我会发现最终问题......
Thank you so much.非常感谢。
Thanks for the playground link, it helped to find the issue.感谢游乐场链接,它有助于找到问题。
It was not working because you want to store a factory, asked for an instance.它不起作用,因为你想存储一个工厂,要求一个实例。
The main trick is here:主要技巧在这里:
registeredTypes: Map<string,typeof ComponentBase>
The full fixed version is here:完整的固定版本在这里:
export class ComponentBase {
prop: string;
constructor(prop: string) {
this.prop = prop;
}
}
export class Component extends ComponentBase {
otherProp: string;
constructor(prop: string, otherProp: string) {
super(prop);
this.otherProp = otherProp;
}
}
export class ButtonGeneric extends Component {
buttonProp: string;
constructor(buttonProp: string) {
super('prop', 'otherProp');
this.buttonProp = buttonProp;
}
}
export class ButtonSpecific extends ButtonGeneric {
buttonSpecProp: string;
constructor(buttonSpecProp: string) {
super('buttonProp')
this.buttonSpecProp = buttonSpecProp;
}
}
export class ComponentFactory {
registeredTypes: Map<string,typeof ComponentBase>
constructor() {
this.registeredTypes = new Map<string,typeof ComponentBase>();
}
register(className: string, classConstructor: typeof ComponentBase) {
if (!this.registeredTypes.has(className)) {
this.registeredTypes.set(className, classConstructor);
}
}
create(className: string, properties: string) {
if (!this.registeredTypes.has(className)) {
throw Error('The class [' + className + '] doesn\'t exists. Couldn\'t create new object');
}
let classConstructor = this.registeredTypes.get(className);
if (classConstructor == null) { throw new Error('') }
const instance = new classConstructor(properties);
return instance;
}
}
const FactoryButtonConst = 'button';
export class ButtonFactory extends ComponentFactory {
constructor() {
super();
this.register(FactoryButtonConst, ButtonSpecific);
}
createButtonSpecific(buttonSpecProp: string) {
return this.create(FactoryButtonConst, buttonSpecProp);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.