With TypeScript, how do I enforce a class implements the right function spec?

Viewed 425

The following bit of TypeScript compiles without error:

interface IPerson {
    name: string;
}
interface IVehicle {
    model?: string;
}
interface IClass {
    doSomething(person:IPerson): string;
}

class MyClass implements IClass {
    doSomething(car:IVehicle):string {
        return car.model;
    }
}

but I want it to raise an error because MyClass.doSomething accepts an IVehicle but it should instead accept an IPerson. How do I make the compiler enforce that the class's method accepts the right type of arguments?

2 Answers
Related