-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
T
should be assignable to A extends B ? C : D
if it is assignable to both C
and D
#26933
Comments
In the comment under use cases this is given as an example: type Foo<T> = T extends true ? string : "a";
function test<T>(x: Foo<T>, s: string) {
x = "a"; // Currently an error, should be ok
x = s; // Error
} But am I right in saying that if |
@jack-williams Good catch. That example is wrong, but I believe the |
One possibility might be (ignoring distributive conditional types):
where Though I agree that a rule that works for closed types (without
Is there a way to cleanly flag this to the typechecker? |
We may as well set the contravariant occurrences to their constraints, not to
We'd just specify this for each built-in type constructor. E.g., a primitive type, an object type, or |
Yep, good point.
In that case I think it has to be an error because we're left trying to guess the exact type the parameter will be instantiated to.
Makes sense to me. I'm mainly just wondering if there is a way to signal a distributive conditional type will never be short circuited with
I don't really have enough experience to say whether a distributive conditional type that does not short-circuit is useful in practice, but from an assignability POV it makes things easier. |
assignable to both C and D. (With some restrictions.) Fixes microsoft#26933.
Enough people seem to be running into this, let's get something basic in place. I submitted #27589. |
Could we extend the keywords of this issue with |
@s0 I think you've just done so. |
derp. comments are searchable, you're right! 😂 |
Adding an example just to make the point clear, I hope this can be looked at as this is quite an old issue type Param<T> = T extends undefined ? {
first: string,
} : {
first: string,
}
function run<T>(param: T) {
const p: Param<T> = { // error assigning here
first: 'hi'
}
return p;
} Playground link |
Search Terms
conditional assignable both
Suggestion
A type
T
should be assignable toA extends B ? C : D
if it is assignable to bothC
andD
. Previously mentioned here and here, but it doesn't appear that anyone ever filed an issue to track this.It's unclear what to do if
C
referencesinfer
type parameters, but even an assignability rule that works only whenC
does not referenceinfer
type parameters would be useful.If the conditional type is distributive, then the rule only applies if
A
is known not to benever
.Use Cases
See this comment for one.
Examples
TBD
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: