-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
prefer-native-coercion-functions
should ignore type guards
#1857
Comments
prefer-native-coercion-functions
should ignore type guards
I agree that this should work: const mixedData: (string | null)[] = ['abc', '', null, 'xyz'];
// Eslint is unhappy, but `null` is removed from type
const typedMixedData: string[] = mixedData.filter((d): d is string => Boolean(d));
Note that this example however is wrong, |
related: there's a request in TypeScript to use boolean as a type guard: microsoft/TypeScript#16069 |
Would be nice to have this ignore ones with type guards if possible indeed because the rule seems useful. There seems to be a new PR to fix this on TS but it's been years and years so could be good to fix this for older TS versions. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
@fregante what do you mean microsoft/TypeScript#57465 (comment) |
Yeah, that's my bad. This is the issue to track: microsoft/TypeScript#16655 |
prefer-native-coercion-functions
is used to simplify methods likefilter
that are simply trying to find truthy values. However the native type castings do not provide any typescript assertions, so the resulting type of the filter is unchanged. When manually providing these assertions,prefer-native-coercion-functions
should not suggest a native coercion over the manual callback.prefer-native-coercion-functions
Ideally that second example is ignored by this eslint rule because the custom assertions provide context that the native coercion function does not.
Alternatively the resulting array can just be manually typed casted, but I would argue that typescript's default typings via assertions is preferred.
Similarly I could provide some method like
isTruthy
but that will interfere with
no-array-callback-reference
.The text was updated successfully, but these errors were encountered: