为什么打字稿不能检查界面方法的参数

时间:2017-06-23 16:50:34

标签: typescript interface

我有一些像这样的代码

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) { }
}

我希望打字稿会给我一个错误,比如方法" setTime"应该有两个参数,但它不会发生。为什么呢?

1 个答案:

答案 0 :(得分:0)

如果您将它们声明为内联,则会看到如下错误。接口提供了一个基础,如果你覆盖它认为你没有声明变量(函数)的类型,因为类型不匹配

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'.