Skip to content

Generic type is fully incompatible with conditional type #9305

@mwiencek

Description

@mwiencek

Flow version: 0.278.0

Expected behavior

function fn<T: string>(str: T): void {
    str as (T extends string ? T : T); // works
    str as (T extends number ? T : T); // should work but errors
}

Actual behavior

3:     str as (T extends number ? T : T); // should work but errors
       ^ Cannot cast `str` to conditional type because string [1] is incompatible with conditional type [2]. [incompatible-cast]
    References:
    1: function fn<T: string>(str: T): void {
                                   ^ [1]
    3:     str as (T extends number ? T : T); // should work but errors
                   ^ [2]

I understand this is a very contrived example, but I was trying to come up with a minimal reproduction that demonstrates the issue clearly. We've hit this in real code, and I used something closer to a real example in the Try-Flow below.

  • Link to Try-Flow or Github repo: Try-Flow

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typing: completenessNo false positives (type checker claims that there are some errors in the correct program)bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions