#Wrong overload being chosen?
8 messages · Page 1 of 1 (latest)
Preview:```ts
export function useEventListener<
T extends Document,
K extends keyof DocumentEventMap
(
observer: T,
...args: Parameters<
typeof document.addEventListener<K>
): void
export function useEventListener<
T extends Window,
K extends keyof WindowEventMap
(
obse
...```
Seems like it doesn't try to decide until you have enough args. Maybe that is a preliminary check, so it doesn't test until that is met
Hm. I think you're right. That's really quite annoying and some bad UX
I tried it with some random types and no .... Seems to behave similarly unfortunately
Preview:ts export function foo( arg0: string, arg1: number, arg2: boolean ): void export function foo( arg0: number, arg1: boolean, arg2: string ): void export function foo( arg0: boolean, arg1: string, arg2: number ): void export function foo(...args: any[ ...
Seems like the only way around it that I can find is to combine the overloads: https://www.typescriptlang.org/play?#code/FAUwHgDg9gTgLgAgGYFcB2BjOBLKblRQA8AKguHCGgCYDOCtcM2aA5ggD4JooC2ARiBicE-QgBsQAQzQA+ABTAECKTFYAGAFwISAGiUq1ARm1kKVOgyYt2Afm59Bw0+TCUa9HgKEJ7YqJIyCNqMzGz6yqqsAEwu5h5WYXaiEtL4cW4Wno4+9qE2wQ7eMPoAlNoAblDY1AgA3gC+wMBIhPIARO26CEA
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@celest cairn Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
export function foo<T extends string | number | boolean>(
arg0: T,
arg1: T extends string ? number : T extends number ? boolean : string,
arg2: T extends string ? boolean : T extends number ? string : number,
): void {}
foo("", ```