-
Notifications
You must be signed in to ch 8000 ange notification settings - Fork 7.1k
.filter() typing breaks on unions of arrays #5928
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
When you use the native Array.prototype.filter() function on a union type (like Fizz[] | Buzz[]), TypeScript is more lenient and tries to infer the type based on the structural typing principle. Structural typing means that TypeScript checks if a type has the necessary properties at runtime, rather than enforcing strict type rules upfront. TypeScript assumes that both Fizz and Buzz might have an id property, and it allows you to proceed without throwing an error. This leniency can lead to runtime errors if one of the types (e.g., Buzz) doesn't actually have the id property. Lodash’s TypeScript type definitions are stricter because they want to ensure you don’t encounter runtime issues by accidentally accessing properties that don't exist on certain types. So you can use something like: interface Common { interface Fizz extends Common { interface Buzz extends Common { _.filter([] as Fizz[] | Buzz[]).filter(item => item.id < 5); Now _filter can be sure that both Fizz and Buzz has a id property. |
The following code errors for lodash but not for the similar implementation using the built in
.filter()
. I encountered this, but my minimal repro is inspired by @ialexryan 's report of microsoft/TypeScript#44373. This is also true forfind
,every
,some
, etcor view on TS Playground
Note: Typescript would error as well until this was fixed by Typescript 5.2.2
The text was updated successfully, but these errors were encountered: