Skip to content

Generics in union should be inferred as never when possibleΒ #62248

@zeng-y-l

Description

@zeng-y-l

πŸ”Ž Search Terms

generics, inference, union, never

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about union and never.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAZjAvDAPAFQDQD4AUAPALhjRgB8ZoAnASzAHMMYBPIk8sAVwFsAjAU0oBKJFmYAoODgDkAQymMAjMIgyo1CHGp8IMTrwEwA9IeIx1MAEQyLukAHcxY0JFh0kqEnzxQ+YACY6HP58mmB8friEpuzc-EIiMHhidDhKFKrqmtq6sQbGpuZBfiG04bZ2QA

πŸ’» Code

const f = <T,>(x: T | string, y: T | number) => y
f('a', 1) satisfies number // T is "a" now

const g = <T extends undefined>(x: T | number) => x
g(1) satisfies number // T is undefined now

πŸ™ Actual behavior

T is not never.

πŸ™‚ Expected behavior

T should be inferred as never, because it can be anything.

Additional information about the issue

This may be related to #42491.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions