Skip to content

Optional parameter needs to be provided #59838

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
gulewei opened this issue Sep 3, 2024 · 1 comment
Closed

Optional parameter needs to be provided #59838

gulewei opened this issue Sep 3, 2024 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@gulewei
Copy link
Contributor

gulewei commented Sep 3, 2024

🔎 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

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgKIDcLgLJwA7IDeAUMsnAFzIgCuAtgEbQDcpyDV6A9sACasBfYsV4QEAGzhQUMGiARhgXEMl7AAznjhgEACwA8AaWQQAHpBC91yANYQAnlxhpMOfAD4AFGHt4IVQwAaZDApAHMIMCoMLDBcPABtQwBdAEpOHn4RDS0dXU8AcjgC4IBGACYAZlTWNU1tPUKGAprkAHo2tFM-BQheZHLyKDD6WPVghhowZDCuadKAOk9ygFYVgBZU5ABBFXDR8GQYLihkAtDhyILkAHc4axA55DwoLnQ+PuEfP2QAVRAlCAACpcACS4Gg6jEimU+l+7mQAF42J5fiZzFgrOQQPZkAB+ZCeUxUX5bREI7h8ZBUEAQTBQLZmCxYzxEqigGDQZCgskUzJbAmg6nUOnQYjfFAAERyDQMxkRtgcThcsXiCIV-0BIPBkCgUIUgKM6OZ1jsjmcMTcBAJ3l8-mQQRC4Ui0VccXwSTSSL5VJpoqg7lqYkk0mQCGU6mmdVyenKVGl9TytRleXKhWKZSqNWyidjTRazHanS4NmIQA

💻 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?

@jcalz
Copy link
Contributor

jcalz commented Sep 3, 2024

duplicate #29131

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 3, 2024
@gulewei gulewei closed this as completed Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants