我有一些像这样的代码
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"应该有两个参数,但它不会发生。为什么呢?
答案 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'.