Microsoft/TypeScript
在 GitHub 查看Mixins created from `obj.constructor` cannot have members
Open
#61,255 创建于 2025年2月23日
Domain: classesHelp WantedPossible Improvement
仓库指标
- Star
- (48,455 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 9 个 PR)
描述
🔎 Search Terms
mixin constructor property instance
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about mixins
⏯ Playground Link
💻 Code
type Constructor<T> = new(...args: any[]) => T;
class Foo {
// Work around https://github.com/microsoft/TypeScript/issues/3841
declare ["constructor"]: Constructor<this>;
}
function Mixin1<T extends Constructor<Foo>>(superclass: T) {
// OK
return class extends superclass {
foof() {}
};
}
function Mixin2<T extends Foo>(obj: T) {
// 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 'Foo'.
return class extends obj.constructor {
foof() {}
};
}
🙁 Actual behavior
Mixin1 compiles fine, but Mixin2 has an error:
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 'Foo'.
Removing the foof member causes it to compile without error.
🙂 Expected behavior
Both should compile fine because they are doing the same thing.
Additional information about the issue
No response