Unexpectedly, the code below compiles without any errors (using tsc 3.9.5):
interface IDateHandler {
handleDate: (Date) => void;
}
let dateHandler: IDateHandler = {
handleDate: (d: Date) => {},
};
dateHandler.handleDate([1, 2, 3]);
Even more surprising is that if handleDate([1, 2, 3])
is replaced with handleDate([1, 2, 3], 4)
, tsc
throws
error TS2554: Expected 1 arguments, but got 2.
Clearly, it recognizes the function signature of handleDate
, yet it seems to be disregarding the actual types.
Even more surprisingly, removing the interface declaration causes type checking to work correctly:
let dateHandler = {
handleDate: (d: Date) => {},
};
dateHandler.handleDate([1, 2, 3]);
// error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'Date'.
// Type 'number[]' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
What's happening here?