Skip to content

Conditional type doesn't go to true or false branch.Β #48243

Description

Bug Report

πŸ”Ž Search Terms

generic conditional types

already checked https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs and #31751

πŸ•— Version & Regression Information

3.9.7 / 4.6.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Action<P, T = never> = { type: string; payload: P } & ([T] extends [never] ? { metaSkipped: true } : { meta: T });

function prepareAction<P, T extends Record<string, unknown> = Record<string, unknown>>(action: Action<P, T>): void {
    action.meta // Property 'meta' does not exist on type 'Payload<P, T>'.(2339)
    action.metaSkipped  // Property 'metaSkipped' does not exist on type 'Payload<P, T>'.(2339)
}

πŸ™ Actual behavior

properties from intersection are not accessible (conditional type doesn't resolve as true / false branch)

πŸ™‚ Expected behavior

conditional type should go to false branch. correct my please if i'm wrong

Thanks in advance.

Activity

  1. MartinJohns commented on Mar 14, 2022

    @MartinJohns
    Contributor

    Resolving of conditional types involving unbound generic type arguments is deferred. At that point the compiler doesn't know what Action<P, T> resolves to, because the compiler doesn't know what types P and T are.

  2. alexeymolchan commented on Mar 14, 2022

    @alexeymolchan
    Author

    Martin Johns (@MartinJohns) thanks for clarification, but i still don't understand. why it doesn't know what result of conditional type if i'm telling it that T will extend Record<string, unknown>

  3. MartinJohns commented on Mar 14, 2022

    @MartinJohns
    Contributor

    T can still be never, as never also extends Record<string, unknown>. TypeScript simply does not resolve conditional types involving unbound generic types, even if theoretically in some specific corner cases it could.

  4. alexeymolchan commented on Mar 14, 2022

    @alexeymolchan
    Author
  5. RyanCavanaugh commented on Mar 14, 2022

    @RyanCavanaugh
    Member

    if i'm telling it that T will extend Record<string, unknown>

    Conditional types are not necessarily linear (meaning that they have predictable behavior between T and an a subtype of T), and figuring out whether or not they are requires reasoning in the form of "Does any type exist such that this type would behave in the other way?", which is not very tractable.

  6. alexeymolchan commented on Mar 14, 2022

    @alexeymolchan
    Author

    Ryan Cavanaugh (@RyanCavanaugh) okey, but why [T] extends [never] and T extends never produce different results (you can see the example above) ?

  7. fatcerberus commented on Mar 14, 2022

    @fatcerberus

    Aliaksei Molchan (@alexeymolchan) https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types

    T extends ... distributes over unions. [T] extends [...] does not. The former is thus more likely to be deferred than the latter, since it can't know whether to enable the distributive behavior or not until it has a concrete type to work with.

  8. alexeymolchan commented on Mar 14, 2022

    @alexeymolchan
    Author

    Bruce Pascoe (@fatcerberus) as i see from playground where T extends ... no defer occur and i can access action.meta. correct me please, if i'm wrong.

  9. alexeymolchan commented on Mar 14, 2022

    @alexeymolchan
    Author

    Martin Johns (@MartinJohns) mapped type like this type OptionsFlags<Type> = { [Property in keyof Type]: boolean; }; will be deferred too ?

  10. MartinJohns commented on Mar 14, 2022

    @MartinJohns
    Contributor

    How would the compiler know what the final type will look like without knowing what type T actually is?

  11. fatcerberus commented on Mar 14, 2022

    @fatcerberus

    To be clear, generics in TS don't work like C++ templates. The compiler only looks at the body of a generic once and tries to determine if it will work for all possible combinations of type parameters, but these checks are necessarily conservative because it would be prohibitively expensive and/or complex to do otherwise.

  12. typescript-bot commented on Mar 17, 2022

    @typescript-bot
    Contributor

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

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

    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