-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Type inference considers only type amendments (if any are present), ignoring original typings #59576
Comments
The original typings are considered, but the newly-added overload in this case is chosen when there's no other inference information available. I think you want to augment the underlying map instead here interface WindowEventMap {
customEvent: Event & { neat: string };
}
window.addEventListener("customEvent", x => {
x.neat; // works
}); |
@RyanCavanaugh thank you for this suggestion! I'll offer using this way of adding new event type to |
It'd take a very long time to step through the inference process and figure out what all the candidates are, etc.. The example is reproducible without interface augmentation, so is therefore not related to it: interface Thing {
// Your definition (goes in first):
addEventListener(type: 'customEvent', listener: (this: Window, event: Event) => unknown, options?: {}): void
// lib.dom.d.ts definitions:
addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
}
interface GlobalThing {
addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
}
// Local version
declare const someThing: GlobalThing & Thing;
someThing.addEventListener('message', () => {})
addListener(someThing, 'message')
addListener<'message', Event>(someThing, 'message') |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
type inference amended types
π Version & Regression Information
β― Playground Link
Link
π» Code
π Actual behavior
TS-2345
error in the lineaddListener(window, 'message')
:Argument of type '"message"' is not assignable to parameter of type '"customEvent"'
.It is wrong because Typescript infers
TEventName
ascustomEvent
, considering only type amendment and ignoring original typings.π Expected behavior
No errors are expected, because original typings for
Window
should also be consideredAdditional information about the issue
I have a generic utility function that subscribes to certain event of provided event source, it looks like one in the example.
I use this utility function including for subscribing to events of
window
. After vite added its own type amendment forWindow
interface, errors in my code occurred, because in the case there are any type amendments, Typescript doesn't consider original typings forWindow
interface during type inference.The text was updated successfully, but these errors were encountered: