-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Valid filter predicates rejected after 5.2.2 upgrade #53489 #55777
Comments
It is predicates in the plural. So it will be either element It's up to the user implementation to implement that correctly. Are we on the same wavelength now? |
In isolation, each of the Really the type of
|
π Search Terms
Add fallback logic for generating signatures for unions of array members #53489
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.2.2#code/CYUwxgNghgTiAEYD2A7AzgF3rGaUC55MYBLFAcwG0BdeAH3hQFcBbAIxBhoG4AoX0JFgIAZkxRgMJVPBYBPAApxgJMFAwgAFADcoEJiELN2nADTwyoAB5HWHGOZxQ5tk12oBKQmyRIIIKBQ+QWg4eDEJKRl5JRAVNQ0dPQNCYjJyc0sQG0Y7M2wYGGdUjFIKGi94Hz8AoP5kdCwYAEZ4AF4C3BQAOhESCA0YTRjlVXUQDz5eAHpp+ABWbubugDZ4ThgkGEJNGT6Bzg8ZuYAVAAsSNHWrAAc4NDRpFAsrlCQsNQhoNn9u4-h4ABRKBgM6yEBueBIETwDBnBDiJ6wuQ3BD-eAAcgA3vAADwAZWuGhQwCuaQoAD5NHc4mMNDtdPpDERSulMiTsq57I5CsUWWUqJ52hT4IyDC94PjzHDLgBBGDkAD8hECckq+J48HRAPg1NGCWZSSZJQF7OsXPyThc-PSFWF8HEAGs3gB3FDSi5oeVKlUoNUm23UbjwAC+9HgOO6UeDIYxWtmOrOUDJJHIKHUTHu5jYTCwbxQCGhsLOSDQCEeaYz9wKCGQLBu6hIPwQLpIcPWILB73hMG6mgATABmAAsAE4jv9Fv3uv31oUtjtoop9eMjgmAHJIKHaTgQJBQYCydSgkBXGVXT4QP4JgEAeR3MD3B-grSL-fMGL1tINDOSzPJ5DhsY3IWByOTAZavLWpoAFAXkMAeHabQimKCCXDaFBwW4Hpyg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QPJ6ISRTyU8EAA
π» Code
Passing valid predicate types to
Array.filter
is an error in version 5.2.2.The following code produces errors in both version 5.1.6 and 5.2.2,
but the errors are are different and specify different locations.
In 5.1.6 the error is on
filter
, in 5.2.2 it is onmyPredicates
.π Actual behavior
Error, as shown above.
Also, the return type
(string|number)[]
is not correct - it should bestring[]|number[]
. I understand that the overly widened return type was thought to be better than nothing, However, since the new behavior in 5.2.2. is still buggy, perhaps the final fix could could also return the correct type.π Expected behavior
Emit no error.
Additional information about the issue
Note that using a template function does work with current 5.2.2 version:
However, the behavior described by passing the overloaded
myPredicate
template function as not the same is not the same as the behavior described by passingmyPredicateTpl
. Only the former describes the scenario such that the same type is used for all index values and that the array has single valued element type.We can actually generate a working version of this using the following distributed template function:
It also actually returns the correct type
string[] | number[]
.Here we can see it describes a union of signature types:
While currently TypeScript has the unfortunate limitation of only being able to return
string[] | number[]
,it may at some point be improved to overcome that limitation and return the accurate type. For that reason
it is better to encourage habitual use of a predicate description which actually describes the behavior of the predicate.
I guess putting a distributed template type like
ArrayFilterFunctionType
in a TypeScript lib file would be probably be too computationally expensive. However, if the same thing could be achieved with oneof -like notation and implementation, then it could become cheap enough.Finally, note that adding a catch all function with return type
never
to the overaloads will get rid of the error:However, it returns
(string|number)[]
, not the correct return typestring[] | number[]
.The text was updated successfully, but these errors were encountered: