Daniel Sun
Daniel Sun

Reputation: 90

Why typescript dose not check the param of interface method

I have some code like this

interface ClockInterface {
    currentTime: Date;
    setTime(d: Date, d2:Date);
}

class Clock implements ClockInterface {
    currentTime: Date;
    setTime(d: Date) {
        this.currentTime = d;
    }
    constructor(h: number, m: number) { }
}

I hope typescript will give me an error like the method "setTime" should have two param, but it does not happen. Why?

Upvotes: 1

Views: 65

Answers (1)

azyth
azyth

Reputation: 688

If you declare them inline you will see an error like this. the interface provides a basis and if you override the type it thinks you are not declaring that variable(function) since the types do not match

setTime:(a:Date,b:Date)=>Date;
setTime = (a: Date)=> { 
  return a;
}

Subsequent variable declarations must have the same type. Variable 'setTime' must be of type '(a: Date, b: Date) => Date', but here has type '(a: Date) => Date'.

Upvotes: 2

Related Questions