打字稿:使用错误的参数计数没有警告

时间:2019-11-22 08:32:22

标签: typescript tslint

我当前正在使用Typescript 3.6.4。

代码:

const a = () => {
  console.log("what ever");
}
const b = (aprop:(aparam:string)=> void) => {
  aprop("myparam");
}
const c = () => {
  b(a)
};

c()

以某种方式,TS并未显示任何错误对此进行编译。尽管“ a”不带参数,但“ b”可以使用参数轻松地调用“ a”。

但这会引发错误(预期参数为0,但得到1):

const a=()=>{
    console.log("what ever")
}

const b=()=>{
    a("x");
}

对我来说,这似乎是一个错误,但也许我缺少这里的东西。

1 个答案:

答案 0 :(得分:1)

已更新

const a: (a: string) => void = () => {
    console.log("no matter");
}

请参阅https://www.typescriptlang.org/docs/handbook/type-compatibility.html#comparing-two-functions

() => void(a:string) => void兼容,因为前一个函数不需要参数。
调用该函数时不会使用该参数,也不会导致错误。

相关问题