Definitely assignable relation should consider some type variable constraints #25883
Closed
3 of 4 tasks
Labels
Duplicate
An existing issue was already created
Search Terms
definitely assignable conditional type variable constraint
Suggestion
The "definitely assignable" relation used to test the condition of a conditional type currently doesn't consider any type variable constraints.
checker.ts
gives the motivating example:I assume the concern is that immediately resolving to the true branch would be incorrect if
T
is later set to{x: number}
. The problem arises from the nontransitivity of assignability;any
is the most obvious source of nontransitivity, but there are some others, such as optional properties. Giventype Foo<T extends A> = [T] extends [B] ? C : D
, in many cases in whichA
extendsB
(at least whenA = B
), it should be possible to reason that everyT
that extendsA
also extendsB
, or at least implement something that is good enough by TypeScript standards of soundness.Use Case 1
When using a library in the spirit of https://github.com/gcanti/fp-ts/ that defines existential types and we want an existential of a generic type whose type variable is constrained. Came up in real code here; I was unable to use the library and had to write an existential type by hand for the purpose. Minimized version:
Use Case 2 (obsolete if #25879 is implemented)
When using the "generic index" workaround described in #25879, I currently have to weaken the constraint of any type variable that needs to accept a generic index, e.g., in
type SpanId<A extends AxisL>
. I'd like to avoid this by using a conditional type, but I'm foiled by the definitely assignable relation not considering the constraint:Examples
See the use cases for two examples of what would be allowed that currently isn't. I'll wait to see if this suggestion gets any interest before putting forth a proposal beyond "at least
A = B
should work".Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: