-
Notifications
You must be signed in to change notification settings - Fork 12.8k
function ...args with generic union narrows incorrectly after second param #31413
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
Also worth note, I only started seeing this issue once I upgraded from 3.3.1 -> 3.4.x |
also, I know the MRE doesn't make sense, but it's the simplest way I could reproduce the error |
Before I address the issue, let me ask a question. Are class fieldBuilder<T>{
public select<F extends FunctionField<T>, P extends QueryField<T>>(...args: Array<P | F>) {}
} If not, why not write the signature like this: class fieldBuilder<T>{
public select(...args: Array<QueryField<T> | FunctionField<T>>) {}
} In general, type parameters should always be kept to a minimum. It is suspicious to have type parameters that don't occur both in an input position (i.e. parameter) and an output position (i.e. return type or callback parameter). And it is particularly suspicious to have type parameters that only occur once in a signature (since you might as well replace them with their constraint). |
@ahejlsberg I came to that realization after submitting this (which is part of the reason I said the MRE doesn't really make sense). Just typing the parameters directly does fix the issue. I didn't close it because it still seemed like a bug? If not, feel free to close this out |
So, the reason it worked in earlier versions is because we failed to make any inferences for |
TypeScript Version: 3.4.x (latest ts-playground)
Search Terms:
...args with generic to narrow
Code
Expected behavior:
I would expect that my
...args
array could be any number of items fromFunctionField<T>
orQueryField<T>
...Actual behavior:
As soon as I mix the types, it starts to narrow the
QueryField<T>
based on the first type passed it.Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: