Skip to content

Optional parameter needs to be provided #59838

Closed
@gulewei

Description

@gulewei

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions