-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Regression in conditional type predicate #51242
Comments
Putting up a repro for the bisect bot declare function f4<T>(arg: T): arg is T extends any ? {} extends T ? T & any[] : Extract<T, readonly any[]> : never;
function case1<T extends any>(a: T | T[]): T[] {
return f4(a) ? a : [a];
}
function case2<T extends unknown>(a: T | T[]): T[] {
return f4(a) ? a : [a];
}
function case3<T>(a: T | T[]): T[] {
return f4(a) ? a : [a];
} |
I believe the root cause here is that the implicit constraint of unconstrained generics behaves closer to |
The change between release-4.5 and main occurred at 2c68ded. |
Absent a strong argument for any particular thing being a defect, I think the right answer here is just that |
@RyanCavanaugh Not sure if I interpret anything wrong, but writing How about the cases with |
This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
Search Terms: type assertion, type comparison, related to, generic parameter
Version & Regression Information: This changed between versions 4.7 and 4.8
Code: Playground Link
I discovered this issue in my PR #50454 when I am looking for a possible solution to the typing of
Array.isArray
.Consider the following code:
In the above,
f4
is the simplified version of the type I used in the PR.Actual behavior:
From 4.8, the results vary from function to function. In case 3, an error occurs when
f
isf4
but not in case 1 and case 2 or the case whenf
isf2
whereT
is not distributed usingT extends any
.Here is a table of whether a ts2322 error occurs in each cases:
For the latter half,
f(n+8)
is simplyfn
withExtract
expanded out, but I could hardly understand why the behavior changes:(Tested in v4.9.0-dev.20221020 and 4.8.4)
Prior to 4.8, ALL the cases passed:
(Tested in 4.7.4 and 4.6.4)
(Note: some of the test functions above are for control (i.e. comparison) only.)
Expected behavior:
For each row, the results of case 1, case 2 and case 3 should be identical. (
f4
,fc
,fd
)For each column, the results of
fn
andf(n+8)
should be identical. (f5
,fd
,f7
,ff
)I understand that some of them must be desirable changes, so I am not expecting the results to be the same between 4.7 and 4.8, but the mismatched results due to the absent of generic constraint are really weird.
The text was updated successfully, but these errors were encountered: