Closed
Description
🔎 Search Terms
"ts2554", "optional parameter", "overloads"
🕗 Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs___
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
interface EventMap {
a: number;
b: void;
}
declare function dispatch<K extends keyof EventMap>(type: K, target: EventMap[K]): void;
dispatch('a', 123);
dispatch('b'); // Expected 2 arguments, but got 1.(2554) An argument for 'target' was not provided
type UnionToIntersection<U> =
(U extends any ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never
type Dispatch<K = keyof EventMap> = UnionToIntersection<K extends keyof EventMap ? (type: K, target: EventMap[K]) => void : never>;
declare const dispatch2: Dispatch;
dispatch2('a', 123);
dispatch2('b'); // ok
🙁 Actual behavior
Errors in code
Expected 2 arguments, but got 1.
🙂 Expected behavior
No Errors
Additional information about the issue
dispatch('b')
matches the signature function dispatch<"b">(type: "b", target: void): void
but still raises an error.
I tried use overloads as a work around, and that just works. So is this is a bug or intentional?