0

I came across unexpected behaviour in TypeScript, and would like to understand the typing system better through this example.

Illustrating the example, we define a main function with a parameter which takes in a function with two arguments. We pass in a function which takes in no arguments as the default.

function noop(): void {
  console.log("I still work!");
}

function main(cb: (a: number, b: string) => void = noop) {
  cb(0, "0");
}

main();

Console:

I still work!

My expectation is that the linter would complain that noop takes in no arguments. So why does this behaviour still work?

(I was originally using export default (...args any[]): any => {} as my noop function from therealemjy/noop-ts, which adds to my confusion: if ...args is needed in the package, why does my simpler argument-less noop achieve the same thing?!)

2
  • 1
    See the FAQ entry on why this happens Commented Feb 10, 2021 at 15:51
  • @jcalz Thank you. I wasn't aware this FAQ existed -- this answers my question perfectly (and I'll have a read through the rest -- the type system is very interesting!). Commented Feb 10, 2021 at 15:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.