问题描述
ctx
函数的 loggerOfHidden
参数应该具有什么签名才能工作?
'T' Could be instantiated with an arbitrary type which Could be unrelated to '(Anonymous class)'
错误是什么意思?
Argument of type 'this' is not assignable to parameter of type 'T & Constructor<ISayHidden>'.
Type '(Anonymous class)' is not assignable to type 'T & Constructor<ISayHidden>'.
Type '(Anonymous class)' is not assignable to type 'T'.
'T' Could be instantiated with an arbitrary type which Could be unrelated to '(Anonymous class)'.
Type 'this' is not assignable to type 'T'.
'T' Could be instantiated with an arbitrary type which Could be unrelated to 'this'.
Type '(Anonymous class)' is not assignable to type 'T'.
'T' Could be instantiated with an arbitrary type which Could be unrelated to '(Anonymous class)'
type Constructor<T> = new (...args: any[]) => T;
interface ISayHidden {
sayHidden(): void;
}
class Factory<T extends Constructor<Foo>> {
constructor(public Base: T) {}
ExtendSayHidden(loggerOfHidden: (ctx: T & Constructor<ISayHidden>) => void) {
return new Factory<T & Constructor<ISayHidden>>(class extends this.Base implements ISayHidden {
sayHidden() {
loggerOfHidden(this);
}
});
}
}
class Foo {
hiddenMessage = 'Wow,you found me!';
sayHi() {
console.log('Hi!');
}
sayBye() {
console.log('Bye!');
}
}
const FooExtended = new Factory(Foo).ExtendSayHidden(ctx => {
console.log(ctx.hiddenMessage);
}).Base;
let fooExtended = new FooExtended();
fooExtended.sayHidden();
EDIT:loggerOfHidden
的未来用途将是操作由 ExtendSayHidden
函数生成的扩展实例。请注意,Factory
可以并且将包含多个函数,每个函数都将进一步扩展当前的 Base
类。我只是想让 TypeScript 假装 ctx
不超过当前类扩展的 Foo
,并且能够访问这些字段。如果需要,我可以创建一个更广泛的示例。
EDIT2:更新示例。 TypeScript 可以为 ctx
提供当前类的上下文吗?
type Constructor<T> = new (...args: any[]) => T;
interface ISayHidden {
hiddenMessage: string;
sayHidden(): void;
}
class Factory<T extends Constructor<Foo>> {
constructor(public Base: T) {}
ExtendSayHidden(loggerOfHidden: <U extends Foo>(ctx: U) => void) {
return new Factory<T & Constructor<ISayHidden>>(class extends this.Base implements ISayHidden {
hiddenMessage = 'Wow,you found me!';
sayHidden() {
loggerOfHidden(this);
}
});
}
}
class Foo {
sayHi() {
console.log('Hi!');
}
sayBye() {
console.log('Bye!');
}
}
const FooExtended = new Factory(Foo)
.ExtendSayHidden(ctx => {
console.log(ctx.hiddenMessage);
}).Base;
let fooExtended = new FooExtended();
fooExtended.sayHidden();
解决方法
匿名类中的 this
指的是对象而不是构造函数。
class extends this.Base implements ISayHidden {
sayHidden() {
loggerOfHidden(this);
}
}
但是您使用 T & Constructor<ISayHidden>
将其键入为构造函数/类。因此,您需要更改类型以引用类 Foo
(playground) 的实例化对象:
class Factory<T extends Constructor<Foo>> {
constructor(public Base: T) {}
ExtendSayHidden(loggerOfHidden: <U extends Foo & ISayHidden>(ctx: U) => void) {
return new Factory<T & Constructor<ISayHidden>>(class extends this.Base implements ISayHidden {
hiddenMessage = 'Wow,you found me!';
sayHidden() {
loggerOfHidden(this);
}
});
}
}