Conditional type prevents assignability
See original GitHub issueBug Report
🔎 Search Terms
Index signature assignability, generic index signatures, conditional types
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types
⏯ Playground Link
Playground link with relevant code
💻 Code
// Use of the conditional type `string extends T` prevents assignability.
// However, the conditional type `T extends string` is accepted.
declare class Foo<T> {
bar: string extends T ? number : boolean;
}
declare let indexable: Foo<{[key: string]: boolean}>;
declare let namedProp: Foo<{baz: boolean}>;
indexable = namedProp;
🙁 Actual behavior
When a conditional type is present in Foo
, I sometimes cannot assign Foo<{a: any}>
to Foo<[x: string]: any>
, depending on the condition.
🙂 Expected behavior
Regardless of the conditional type, I should be able to assign Foo
with named properties to Foo
with an index signature.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
TypeScript Conditional Type complains Type not assignable
I am trying to understand how TypeScript conditional type ...
Read more >Conditional type distribution leads to undesirable behavior for ...
I think we have a conflict between "Conditional types should always distribute over unions" and "Primitives (where possible) should behave the ...
Read more >what is the meaning of "is assignable" in the context of "conditional ...
in two scenarios of using extends keyword that I'm aware of ( constraining generic types and conditional types ) extends means "the extends...
Read more >Semantics - The Apache Groovy programming language
the assignment is a variable declaration and A is a list literal and T has a constructor whose parameters match the types of...
Read more >Conditional Types - Advanced TypeScript - YouTube
A conditional type (T extends U ? X : Y) in TypeScript selects one of two possible types based on some condition.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is this on a roadmap or existing issue? I’d like to know whether to wait for this fix, or design around it. Thanks!
Types are not evaluated by iterating over each possible thing the type could be inhabited by, since doing things that way is combinatorially explosive. Things are generally either true for a category, or not, and an operation like this isn’t valid at a category level.