lb0
lb0

Reputation: 33

Typescript: No warning using wrong parameter count

I´m currently using Typescript 3.6.4.

Code:

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

c()

Somehow TS is not showing any error compiling this. Although "a" doesn't take a parameter "b" can savely call "a" with a parameter.

But this would throw an error (Expected 0 arguments, but got 1):

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

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

It looks like a bug to me but maybe there is something I'm missing here.

Upvotes: 1

Views: 163

Answers (1)

Narro
Narro

Reputation: 440

updated

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

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

old

() => void is compatible with (a:string) => void, because the former function doesn't need an parameter.
The parameter won't be used when the function is called, it won't cause error any more.

Upvotes: 1

Related Questions