-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[typescript] lib.dom.d.ts has confusing types #23595
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
Comments
Hmmm.... I see that there already is a related issue: [typescript] dom definitions not up to date #19040 The sad thing is that it is not solved since Oct 9, 2017, but CodeSandbox has done something about it already, so there must be a version of |
The library is generated from https://github.com/Microsoft/TSJS-lib-generator. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes. |
So the suggession here is to have two overloads? interface HTMLDivElement extends HTMLElement {
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, options?: boolean): void;
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, options?: AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean ): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: AddEventListenerOptions): void;
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, options?: boolean): void;
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, options?: EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: EventListenerOptions): void;
} |
Yes, except interface HTMLDivElement extends HTMLElement {
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, useCapture?: boolean): void;
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, options?: AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean ): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: AddEventListenerOptions): void;
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, useCapture?: boolean): void;
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLDivElement, ev: HTMLElementEventMap[K]) => any, options?: EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: EventListenerOptions): void;
} And there are more occurrences of this parameter type, not only To be more precise, 393 occurrences of this particular type definition. If you know how to resolve this with https://github.com/Microsoft/TSJS-lib-generator in a quickly, it would be amazing to have this fix throughout all |
I can confirm that
Do you plan updating the |
Changing a union to overloads is usually much worse from a type system perspective, since it means you can't forward same-signature parameters around. The upside in documentation clarity doesn't seem worth making the types worse here given the low volume of feedback. |
TypeScript Version: 2.8.1
VS Code Version:
Search Terms:
useCapture
addListener
lib.dom.d.ts
Code
Before entering the last parameter (just after writing the last comma in the editor):
I get code completion that looks like this:
And the second overload:
It is very confusing to see
options?: boolean
part not as a separate overload, but as a smushed definition of:Expected behavior:
To have separate overloads for
boolean
version andAddEventListenerOptions
one, as seen in CodeSandbox (Playground link).Actual behavior:
Described above.
Playground Link:
https://codesandbox.io/s/r0l6j9olpn
Related Issues:
Didn't find.
The interesting thing is that while trying to reproduce the issue in CodeSandbox, I got exactly what I wanted to see:

useCapture: boolean | undefined
So, my question is why does VS Code have a version of types that is confusing and can also be found here:
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L5450
And why CodeSandbox has a proper one? And how can we solve this?
Is this VS Code's bug or the
lib.dom.d.ts
bundled with TypeScript is simply out-of-date?The text was updated successfully, but these errors were encountered: