-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inference from rest parameters has strange inconsistent results #37193
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
Comments
This might be a hard thing to fix unless you would be OK with some breakage. The problem is in the Because of this we end up checking if Since this isn't assignable we pick the constraint over the inferred type. It's not easy to change this inferred constraint to Alternatively, Note that I would consider those to have a semantic and not a syntactic match (and this case is also inferrable through type Foo<T extends string, U extends T> = [T, U];
type Bar<T> = T extends Foo<infer X, infer Y> ? Foo<X, Y> : never; Using this "prefer inferred type over inferred syntactical constraint" logic could also fix #46020 and #44143 (according to my local experiment). |
I'm one year more experienced now 😉 and I don't see how this can be fixed without breaking something. The problem is that the implied constraint of those rest params is What can be done about it? I see 2 possible solutions.
type AcceptArr<T extends unknown[]> = T
type A<T> = T extends (...rest: infer P) => any ? AcceptArr<P> : never
I think only option 1 is viable here but it's a breaking change. |
There is also an option number 3. Rest parameters could strip readonly-ness from instantiations. This would feel consistent with rests in tuples: type Test<T extends readonly unknown[]> = T extends readonly [...infer R] ? R : never;
type Result1 = Test<readonly any[]>;
// ^? type Result1 = any[]
type Result2 = Test<readonly number[]>;
// ^? type Result2 = number[] It would also be consistent with the proposed fix for #53398 |
Expected:
FooParams
isany[]
orreadonly any[]
BarParams
isnumber[]
orreadonly number[]
Actual
FooParams
isunknown[]
BarParams
isnever
Playground link
The text was updated successfully, but these errors were encountered: