Skip to content

Contextual narrowing not narrowing on generic map.Β #45234

Closed

Description

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare const eventType: keyof DocumentEventMap;

document.addEventListener(eventType, (event) => {
  event; // Here `event` is `Event | UIEvent | AnimationEvent | MouseEvent | InputEvent | FocusEvent | CompositionEvent | ... 9 more ... | ClipboardEvent
  if (eventType === `click`) {
    event; // Expected: `event` to be type `MouseEvent`. Actual: It's the union from above.
  }
});

πŸ™ Actual behavior

Second reference of event is type Event | UIEvent | AnimationEvent | MouseEvent | InputEvent | FocusEvent | CompositionEvent | ... 9 more ... | ClipboardEvent.

πŸ™‚ Expected behavior

Second reference of event to be of type MouseEvent.

Activity

  1. jcalz commented on Jul 29, 2021

    @jcalz
    Contributor

    Generic functions like document.addEventListener() have their type parameters specified when you call them. In the case above, the K type parameter is inferred to be keyof DocumentEventMap and therefore the type of event in the callback is the full union; it is no longer "generic" in the way that #43183 (the contextual narrowing feature) would have any bearing on.

    It seems like what you really want is something like #27808 where the type parameter K is treated as some unknown single member of the keyof DocumentEventMap union... although then the compiler might not even let you call document.addEventListener() with a union-typed eventType.

    This also feels like eventType and event are supposed to be correlated unions but the compiler has no way to track that; which is #30581. (This is where #25051 would have helped, if you could wrap the call to document.addEventListener() in a hypothetical type switch (eventType) {...} block. Oh well!)

  2. whitneyit commented on Jul 29, 2021

    @whitneyit
    Author

    Thanks Joe Calzaretta (@jcalz) for the detailed response! I agree that this is similar to #30581.

    Unlike #30581, I was under the impression that the if check would be sufficient to narrow the type to MouseEvent.

  3. fatcerberus commented on Jul 30, 2021

    @fatcerberus

    Right - the compiler doesn’t understand that eventType and event are interrelated for the purposes of type narrowing.

  4. typescript-bot commented on Aug 7, 2021

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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