function logged<
T extends object,
V extends unknown[]
>(
value: { new(...args: V): T },
context: DecoratorContext
)
{
const { kind, name } = context;
if (kind === "class") {
return class extends value {
constructor(...args: V) {
console.log(`constructing an instance of ${name as string} with arguments ${args.join(", ")}`);
super(...args);
}
}
}
return;
}
@logged<C, [n: number]>
class C {
constructor(n: number) {
void(n);
}
}
new C(1);
On the return class part, I get:
Class '(Anonymous class)' incorrectly extends base class 'T'.
'(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'object'.
It's not immediately obvious how I fix this. Example borrowed from https://github.com/tc39/proposal-decorators