Skip to content

Type 'T' is not assignable to type 'T extends Sub ? Sub : T'.Β #48746

Description

@taralx

Bug Report

πŸ”Ž Search Terms

variance, conditional, subtype, limit

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about variance and conditionals.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Base {
    base: unknown;
}

interface Sub extends Base {
    sub: unknown;
}

function transform<T extends Base>(t: T): T extends Sub ? Sub : T {
    return t;
}

πŸ™ Actual behavior

"Type 'T' is not assignable to type 'T extends Sub ? Sub : T'."

πŸ™‚ Expected behavior

No errors. Either T extends Sub, and so is assignable to it, or it doesn't, and so is assignable to itself.

Activity

  1. MartinJohns commented on Apr 18, 2022

    @MartinJohns
    Contributor

    See #48243, #47633 and probably many others.

    Resolving of conditional types involving unbound generic type arguments is deferred. The compiler doesn't know what type T extends Sub ? Sub : T resolves to, because it doesn't know what type T is.

  2. taralx commented on Apr 18, 2022

    @taralx
    Author

    Any recommendations on how to express this differently that doesn't run into this problem? Perhaps there is a way to express the join/meet operation without conditionals.

  3. taralx commented on Apr 18, 2022

    @taralx
    Author

    FWIW I tried using [T] extends [Sub] to avoid distributive behavior but it doesn't help.

  4. whzx5byb commented on Apr 18, 2022

    @whzx5byb

    Any recommendations on how to express this differently that doesn't run into this problem? Perhaps there is a way to express the join/meet operation without conditionals.

    interface Base {
        base: unknown;
    }
    
    interface Sub extends Base {
        sub: unknown;
    }
    
    function transform<T extends Base>(t: T extends Sub ? T & Sub : T): T extends Sub ? Sub : T {
        return t;
    }
    
    var t = transform({base: 0, foo: 0})
    // ^? var t: { base: number, foo: number }
    var sub = transform({base: 0, sub: 0, foo: 0})
    //  ^? var sub: Sub

    I don't know why T extends Sub ? T & Sub : T is assignable to T extends Sub ? Sub : T since it doesn't meet the conditions mentioned in #46429.
    Seems like a black magic 😎

  5. taralx commented on Apr 18, 2022

    @taralx
    Author

    Maybe syntax-driven? Would make sense.

  6. typescript-bot commented on Apr 21, 2022

    @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.

  7. locked as resolved and limited conversation to collaborators on Oct 22, 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