Why is TypeScript unable to detect that the MadeFromString
class is missing a second boolean argument in its constructor, making it incompatible with the StringConstructable
interface in this code snippet:
interface ComesFromString {
name: string;
}
class MadeFromString implements ComesFromString {
constructor(public name: string) {
// Returns an object of type ComesFromString / MadeFromString
}
}
interface StringConstructable {
new (n: string, w: boolean): ComesFromString;
}
function makeObj(n: StringConstructable) {
return new n('hello!', false);
}
const test1 = makeObj(MadeFromString);
However, in this example, TypeScript correctly identifies that the MadeFromString
class is missing a boolean variable named w
:
interface ComesFromString {
name: string;
}
class MadeFromString implements ComesFromString {
constructor(public name: string) {
// Returns an object of type ComesFromString / MadeFromString
}
}
interface StringConstructable {
new (n: string): ComesFromString;
w: boolean; // ERROR
}
function makeObj(n: StringConstructable) {
return new n('hello!', false);
}
const test1 = makeObj(MadeFromString); // ERROR