This code compiles without errors, although two of the constructors do not match the declared constructor type.
How to detect constructor type mismatch?
type ObjConstructor = new (name: string) => Obj // type of constructor (string) =>
// or by interface — doesn't matter
// interface ObjConstructor {
// new (name: string): Obj
// }
class Obj {
constructor(private id: number) { } // first conflicting constructor (number) =>
print() { console.log(this.id) }
}
class Sub extends Obj {
constructor() { super(0) } // second conflicting constructor () =>
}
function foo(f: ObjConstructor) { // function expects signature (string) =>
const obj = new f('name')
if (obj instanceof Sub) {
obj.print()
}
}
foo(Sub) // wrong signature sent here (number) => or () =>
Link to playground