question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional type prevents assignability

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dylhunncommented, Feb 26, 2022

Likely we’re considering a type extends T to mark Foo<T> as invariant in T

Is this on a roadmap or existing issue? I’d like to know whether to wait for this fix, or design around it. Thanks!

0reactions
RyanCavanaughcommented, Aug 5, 2022

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found