我应该在这个泛型混乱中指定什么签名?
What signature should I specify in this generics mess?
loggerOfHidden
函数的 ctx
参数应该有什么签名才能工作?
'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();
编辑:loggerOfHidden
的未来目的将是操纵由ExtendSayHidden
函数生成的扩展实例。请注意,Factory
可以并且将包含大量功能,每个功能都将进一步扩展当前 Base
class。我只是想让 TypeScript 假装 ctx
不超过 Foo
由当前 class 扩展,并且能够访问这些字段。如果需要,我可以创建一个更广泛的示例。
EDIT2:更新示例。 TypeScript 可以为 ctx
提供当前 class 的上下文吗?
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();
匿名 class 中的 this
指的是对象而不是构造函数。
class extends this.Base implements ISayHidden {
sayHidden() {
loggerOfHidden(this);
}
}
但是您输入的是 constructor/class 和 T & Constructor<ISayHidden>
。因此,您需要更改类型以引用 class 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);
}
});
}
}
loggerOfHidden
函数的 ctx
参数应该有什么签名才能工作?
'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();
编辑:loggerOfHidden
的未来目的将是操纵由ExtendSayHidden
函数生成的扩展实例。请注意,Factory
可以并且将包含大量功能,每个功能都将进一步扩展当前 Base
class。我只是想让 TypeScript 假装 ctx
不超过 Foo
由当前 class 扩展,并且能够访问这些字段。如果需要,我可以创建一个更广泛的示例。
EDIT2:更新示例。 TypeScript 可以为 ctx
提供当前 class 的上下文吗?
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();
匿名 class 中的 this
指的是对象而不是构造函数。
class extends this.Base implements ISayHidden {
sayHidden() {
loggerOfHidden(this);
}
}
但是您输入的是 constructor/class 和 T & Constructor<ISayHidden>
。因此,您需要更改类型以引用 class 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);
}
});
}
}