📅  最后修改于: 2022-03-11 14:48:19.641000             🧑  作者: Mango
// Utility types:
type GetProps = TBase extends new (props: infer P) => any ? P : never
type GetInstance = TBase extends new (...args: any[]) => infer I ? I : never
type MergeCtor = new (props: GetProps & GetProps) => GetInstance & GetInstance
// Usage:
// bypass the restriction and manually type the signature
function GeometryMixin(Base: TBase) {
// key 1: assert Base as any to mute the TS error
const Derived = class Geometry extends (Base as any) {
shape: 'rectangle' | 'triangle'
constructor(props: { shape: 'rectangle' | 'triangle' }) {
super(props)
this.shape = props.shape
}
}
// key 2: manually cast type to be MergeCtor
return Derived as MergeCtor
}