Description
🔎 Search Terms
"optional function inconsistency", "runtime error with function assign", "bivariant", "strictFunctionTypes'
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Why are function parameters bivariant?", "Why are functions with fewer parameters assignable to functions that take more parameters?"
⏯ Playground Link
💻 Code
let a = (input?: string) => input?.slice(0, 1);
let b = () => {};
let c = (input: number) => console.log(input);
b = a; // should not be okay
c = b; // okay
c(3); // runtime error!
// c = a <- type error
🙁 Actual behavior
Assigning a function of type (input?: string) => void
to a function of type () => void
passes the type checker but causes a runtime error.
🙂 Expected behavior
Assigning (input?: string) => void
to () => {}
should trigger a type error because (input?: string) => void
is not a subtype of () => {}
; rather, the reverse is true. As a result, this should prevent a runtime error.
Additional information about the issue
Could this be a known issue, or is it an intentional design choice? If intentional, what is the rationale behind it?