Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexeymolchan opened this issue Mar 14, 2022 · 12 comments
Closed

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

alexeymolchan opened this issue Mar 14, 2022 · 12 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@alexeymolchan
Copy link

alexeymolchan commented Mar 14, 2022

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.

@MartinJohns
Copy link
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.

@alexeymolchan
Copy link
Author

alexeymolchan commented Mar 14, 2022

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

@MartinJohns
Copy link
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.

@alexeymolchan
Copy link
Author

@MartinJohns playground but why this works ?

@RyanCavanaugh
Copy link
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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 14, 2022
@alexeymolchan
Copy link
Author

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

@fatcerberus
Copy link

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

@alexeymolchan
Copy link
Author

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

@alexeymolchan
Copy link
Author

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

@MartinJohns
Copy link
Contributor

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

@fatcerberus
Copy link

fatcerberus commented Mar 14, 2022

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.

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants