typescript allows class to be more restrictive than implemented interface

38 Views Asked by At

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

There are 0 best solutions below