Skip to content

Double implicit cast of optional function arguments allows a callback to receive the wrong types #45666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
keeslinp opened this issue Aug 31, 2021 · 3 comments

Comments

@keeslinp
Copy link

Bug Report

🔎 Search Terms

optional argument cast callback

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about substitutability

There is a FAQ for half of this bug https://github.com./Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters.

⏯ Playground Link

Playground link with relevant code

💻 Code

const test3 = (cb: (arg1: string) => void) => {
  cb("test")
}

const test2 = (cb: () => void) => {
  test3(cb)
}

const test = () => {
  test2((val?: number) => {
    console.log(typeof val) // Expect number but it prints string
  })
}
test()

🙁 Actual behavior

This type checks just fine and prints out "string"

🙂 Expected behavior

This should fail somehow because it is unsound. I'm not sure where it should fail and maybe this is just an accepted bug, but if that is the case then the FAQ should be updated to reflect that you can make unsound behavior by implicitly casting functions with optional parameters.

@MartinJohns
Copy link
Contributor

Duplicate of #13043. Used search terms: callback optional unsound

@keeslinp
Copy link
Author

This doesn't seem to be an issue in practice, and our appetite for breaking changes diminishes every day.

I guess there's no desire to fix this. Thanks for linking the other issue 👍

@MartinJohns
Copy link
Contributor

I guess there's no desire to fix this.

As far as I understood there simple is no good way to fix this, without introducing a whole other can of issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants