-
Notifications
You must be signed in to change notification settings - Fork 12.8k
ReadonlyArray.some does not perform type narrowing #47574
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
Yes, of course, there's no way that which member of an array is of a specific type, but from the code above, in case any of member is specific type then returned, lines below the return statement must not be the specified type. |
I know what I don't think you want There are unfortunately no negated types in TypeScript, so there's no representation of the sort of narrowing that happens primarily when a type guard function returns That means the main problem here isn't some bug in Note that in this case you're fighting against the language. Assuming you want to make progress before waiting for TypeScript to change, you will be much happier if you refactor to |
To supplement @jcalz 's correct response, the key thing is that user-defined type guards (UDTGs) can narrow unions (including in the false branch), but The narrowing of |
@jcalz Thanks for your kind explanations. First of all, I confused |
Bug Report
π Search Terms
type narrowing, array, Array.some
π Version & Regression Information
seems to be every version of typescript shows this behavior
β― Playground Link
Playground link with relevant code
π» Code
I was writing some kind of type narrowing/validating code with Arrays, and found out that array.some could not narrow types!
I embedded playground code with comments and also pasted it down here.
P.S. I already saw #14963 and it says its already fixed with ReadonlyArray, but not worked for me
π Actual behavior
ReadonlyArray.any | ReadonlyArray.some could not filter out specific types with given typeguard functions
π Expected behavior
ReadonlyArray.any | ReadonlyArray.some should filter out specific types with given typeguard functions
The text was updated successfully, but these errors were encountered: