-
-
Notifications
You must be signed in to change notification settings - Fork 366
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
no-array-callback-reference
rule is not compatible with TypeScript type guards
#781
Comments
Console shows the same result on typescript playground |
That's not true:
Screenshot:
Screenshot: I think my example was poor, here is a better one: |
Sorry, I mean complied js code result. |
But the code being validated is the TypeScript one. So I’m not sure how the result is relevant? |
Expect items.filter(((num) => isDefined(num)) as typeof isDefined).map((number) => {
// however, this way it does not work, and "number" can still be undefined
return number + 1
}) ? |
I am not sure what is going on there. I am getting confused. Both of the code snippets I posted are valid. My only concern is that the rule is being triggered in a case where there is no other option. The only option is to do |
It's not only about |
Hi, the issue is described here: microsoft/TypeScript#10734 and here: microsoft/TypeScript#16069. |
@oss6 thanks for finding the proper references! |
I think the rule is renamed to |
no-array-callback-reference
rule is not compatible with TypeScript type guards
I agree that this particular scenario is one where array callbacks is actually nice to use. I'd be interested if the |
I could try my hand at this. My initial plan is to add an option with a name like This should be trivial for cases where the function exists in the same file, such as this: https://astexplorer.net/#/gist/6cb8b6a6683042ce444d2033901e43c5/95af3ecc1cb391cc9b94a37fe455880ab94c0bc9 Does anyone know of any existing rules that rely on the Typescript return type of something that's imported? (in unicorn or anywhere else). I've tried thinking about this for 15 minutes without coming up with someone – but I'm relatively new to writing/reading eslint rules. |
I don't know about this, but you can try check https://github.com/typescript-eslint/typescript-eslint, maybe they do this. |
I met this problem too. I'd like to keep using the rule but I can see no convenient workaround. Would a more practical solution be to let the user configure the list of allowed function names, either for filter callbacks or any callback? Introducing type sensitive lints seems pretty drastic for such an isolated issue. |
The typeguard info is lost because the wrapper function is not a typeguard. If you simply make your wrapper function a typeguard then problem fixed. e.g. items.filter((num): num is number => isDefined(num)).map((number) => {
console.log(number + 1);
}); Though this being said, typeguards probably should be ignored by this rule (probably via an option like @Vages suggests (I would suggest the option be named
It shouldn't make any difference if the function is defined in the same file or not; all the information needed is in the function's type information. If people want this, I can make a PR (I'm the main contributor for eslint-plugin-functional so I'm pretty experienced with working with eslint rules). |
With an experienced eslint-rulemaker like you, @RebeccaStevens, making the extension, I'm quite certain that the lint-rule should just ignore type-guards by default (in hindsight, I think I only suggested it due to a lack of confidence in my lint-rule abilities 😅). Having an option for it seems bloated, and if this actually affects someone negatively (which I really doubt), we could add the option in later. I'm only an ordinary contributor myself, but if you make a PR, I could test it and give some feedback. Just tag me when you add it. PS: I use |
I haven't looked too closely at the rule but I believe currently the rule doesn't use type information. |
I'm not either. Do you have an opinion, @fisker? |
Was there ever any update on this? Currently can work around with disable comment, but would be nice to be able to remove them. |
+1, I'd love to see a fix to this rule so I can stop disabling it on all inline typeguards. |
I am not sure why TypeScript is not inferring the results in the second way.
The text was updated successfully, but these errors were encountered: