-
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
Simplify relationship check for conditional type on target side #46429
Changes from 2 commits
9ea685b
75ab347
3012d35
45a0113
dc9da80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(12,20): error TS2345: Argument of type '"value"' is not assignable to parameter of type 'XX extends XX ? "value" : never'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(28,20): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(29,21): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(33,22): error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'. | ||
Type 'null' is not assignable to type 'null extends T | null ? any : never'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(34,23): error TS2345: Argument of type 'T | null' is not assignable to parameter of type '[null] extends [T | null] ? any : never'. | ||
Type 'null' is not assignable to type '[null] extends [T | null] ? any : never'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(39,3): error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'T extends T ? { x: T; y: T; } : never'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(46,3): error TS2322: Type 'string' is not assignable to type 'Foo<T>'. | ||
tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(63,9): error TS2322: Type '{ a: number; b: number; }' is not assignable to type '[T] extends [[infer U]] ? U : { b: number; }'. | ||
|
@@ -10,7 +15,7 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T | |
Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNotDistributive<Q>'. | ||
|
||
|
||
==== tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts (8 errors) ==== | ||
==== tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts (11 errors) ==== | ||
export type FilterPropsByType<T, TT> = { | ||
[K in keyof T]: T[K] extends TT ? K : never | ||
}[keyof T]; | ||
|
@@ -23,6 +28,8 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T | |
|
||
export function func<XX extends string>(x: XX, tipos: { value: XX }[]) { | ||
select(x, tipos, "value"); | ||
~~~~~~~ | ||
!!! error TS2345: Argument of type '"value"' is not assignable to parameter of type 'XX extends XX ? "value" : never'. | ||
} | ||
|
||
declare function onlyNullablePlease<T extends null extends T ? any : never>( | ||
|
@@ -48,7 +55,13 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T | |
function f<T>(t: T) { | ||
var x: T | null = Math.random() > 0.5 ? null : t; | ||
onlyNullablePlease(x); // should work | ||
~ | ||
!!! error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is interesting - is this actually a distributive conditional type? Why is it deferred? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We always defer if either of the check and extends types contains generics. This is somewhat of a blunt instrument, and we could possibly do better in cases like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And by "contains generics" I mean we defer when |
||
!!! error TS2345: Type 'null' is not assignable to type 'null extends T | null ? any : never'. | ||
onlyNullablePlease2(x); // should work | ||
~ | ||
!!! error TS2345: Argument of type 'T | null' is not assignable to parameter of type '[null] extends [T | null] ? any : never'. | ||
!!! error TS2345: Type 'null' is not assignable to type '[null] extends [T | null] ? any : never'. | ||
} | ||
|
||
function f2<T>(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something @weswigham mentioned in the design meeting is that this also has to gate out distributive conditionals.
It would be good to have a test case here if nothing else fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, this is because let's say you're relating
{x: U, y: U}
whereU extends T
to aT extends Whatever ? { x: T, y: T } : { x: unknown, y: unknown }
, you have to return false because the conditional is going to distribute overT
- so let's sayT = string | number
andU = "a" | 0
, that's{x: "a" | 0, y: "a" | 0}
related to{x: string, y: string} | {x: number, y: number}
, which is clearly false.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(And this kind of relation is precisely what happens during variance analysis of something like
T extends Whatever ? { x: T, y: T } : { x: unknown, y: unknown }
, so we can't afford to get it wrong, otherwise we'll taint the variance-based non-generic results with incorrect results)