Skip to content

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

Closed
@keeslinp

Description

@keeslinp

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions