1

Why does typescript allows MyClass to be more restrictive in the parameter type of someMethod than the interface it implements ?

interface MyInterface { someMethod(n: number|Date):string }

class MyClass implements MyInterface {
  someMethod( d: Date ): string { return d.toDateString(); }  // why is this accepted ?
}

function hi( p: MyInterface ): string
{
  return p.someMethod(10);
}

hi( new MyClass() );  // this will fail at runtime

I was expecting a compile error in the definition of someMethod.

0 Answers0