12
interface test{
    foo(boo:string);
}
class coo implements test{
    foo(){

    }
}

In playGround this doesn't generate and error although the function signature is not as the interface says, the expected behavior of interface is to force the signature..

why is this behavior?

Thanks

Bashar Ali Labadi
  • 1,014
  • 1
  • 11
  • 19

3 Answers3

8

This is interesting. The TypeScript team are quite clever chaps and they decided to do this deliberately.

The idea is that if your function can operate correctly without being passed an argument, it can safely ignore the argument and satisfy the interface. This means you can substitute your implementation without having to update all of the calling code.

The interface ensures that the argument is passed in all cases where you are consuming the interface - so you get type checking on the callers and it actually doesn't matter that your concrete class doesn't need any parameters.

Interface Function Parameter Not Enforced

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    I thought it's too obvious to be a bug.. waiting if someone has another answer then ill mark the answer.. thanks – Bashar Ali Labadi Oct 31 '12 at 12:17
  • Updated based on feedback from the TypeScript team. – Fenton Nov 01 '12 at 09:46
  • 1
    Thanks, but for me I would prefer to have parameters checking to ensure correct implementation, and one can argue by saying if I have a function a(arg1) not in an interface.. in a class lets say.. it wont be callable without arg1 unless arg1 is not required by using '?' .. – Bashar Ali Labadi Nov 04 '12 at 06:58
  • `function v(x:number){}; v();// generates error in play ground ` – Bashar Ali Labadi Nov 04 '12 at 07:00
  • Yes - like I said, the calling code must abide by types and interfaces even if the implementing code chooses to omit a parameter. If you want calling code to be able to skip the parameter, you would make it optional: v (x?: number){} – Fenton Nov 04 '12 at 08:40
  • The purpose of Typescript is to make things "stricter" in a loose language. If your function can operate correctly without being passed an argument, just ignore the arguments. – workwise Mar 05 '21 at 10:02
1

I am not satisfied how Interface doesn't enforce the method signature too. I believe the explanations by Fenton are wrong. The real reason is that Typescript is using "duck typing". No erros with less parameters, but you do get errors if you use more parameters.The long answer can be found here Why duck typing is allowed for classes in TypeScript

In the end, Interface can't fit the role of an abstract class that is extended by an other class. I wouldn't recommend to use Interface with classes but instead better use the word "implements" on an actual class, it does the same without the extra Interface class.

Franz
  • 53
  • 4
0

Typescript uses structural typing. The implemented function can have fewer parameters than the function declaration in the interface but not more.

Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59