Skip to content

Intersection Types that have overlapping properties can fail #49264

Closed
@wdamien

Description

@wdamien

This seems to be the source: #31838

Example:
Try an intersection of 2 types where the same property has inconsistent overlapping types:

type Bar = {
    Prop: string
};

type Works = Bar & {Prop: number}; // Bar & {Prop: number}
type ThisIsNever = Bar & {Prop: null}; // never

It makes sense to me that individual types can be reduced to never, but why can intersecting types with nested overlapping property types get reduced to never?

Playground link: https://www.typescriptlang.org/play?ts=4.7.0-beta#code/C4TwDgpgBAQghgJygXigbwFBW1ACggezAC4oBnYBASwDsBzDAXwG4MMB6dqAdQIQGsyUAGa0IGUJCgBJGsAgIyEAMbAqBGiliIoAMnT4ipGgFcAtgCMFLKJ21J9aQySinL1ieGgA5CADcFWXlFFTUNLXgHA0IXGn9rZlsuSL1oo1d4hEY2O28CYCgIAA9IVQgAE08pbxMAG1qghSVVdU1UFMdnYzramzsAJRUIKgDy40ykqABREtCK0gADDrTYnsYFqAAKPiglnU6Y8YCshYBKKugAVRpyiFE48saQlvD2-ZXSExu7sXK+rkGymGoyOCkmM1K8jGu2WTkOUC+t3uFXWWx2eyicPScWO63OQA

This might be expected, but it does feel like a bug to me. My initial use case was joining a few different types together to get a union of all their keys. But since some props had a null | undefined type, it was reduced to never. Causing my use of those keys to fail unexpectedly. I would expect ThisIsNever to have a Prop: never property instead, or maybe a union of string | null. Or at-least surface an error saying the union is not possible because of Prop being incompatible.

Tested on: 4.6.4, 4.7.0-beta
This worked as expected in: v3.8.3. And starts not being expected in v3.9.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions