-
Notifications
You must be signed in to change notification settings - Fork 1
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
unicorn/prefer-native-coercion-functions
doesn't work well with typescript and should be disabled.
#242
Comments
It seems there is an open issue for this: sindresorhus/eslint-plugin-unicorn#1857 Alternative option is to use But it seems that, until that issue is fixed, we should disable it. |
Typescript now infers type predicates properly, but with this change I think we should disallow the use of the https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#inferred-type-predicates |
Typescript doesn't infer the types correctly for the Boolean constructor. The type guard is a type cast, it's unsafe and shouldn't be the preferred method to narrow the type. const test = [1, 2, 3, undefined];
const hey: number[] = test.filter((item) => item !== undefined); const test = [1, 2, 3, undefined];
const hey1: (number | undefined)[] = test.filter((item) => Boolean(item));
const hey2: (number | undefined)[] = test.filter(Boolean); |
What Leroy says below. |
Well, I think the conclusion is to stop using type guards when it's not necessary. Instead of relying on coercion to narrow a type it's better to implement a stricter condition. Disabling the rule doesn't discourage people from using type guards. |
The Boolean part of this rule and the auto fix break my code, because they don't work well with typescript.
I write this:
and it converts it to this:
This is not the same. The type of output is
Array<string>
in my code andArray<string | null>
in the 'autofixed' code.The text was updated successfully, but these errors were encountered: