`no-array-callback-reference` rule is not compatible with TypeScript type guards
See original GitHub issuefunction isDefined<T>(argument: T | undefined): argument is T {
return typeof argument !== 'undefined'
}
const items = [1, 2, undefined]
items.filter(isDefined).map((number) => {
// number is now type guarded and undefined is eliminated
console.log(number)
})
items.filter((num) => isDefined(num)).map((number) => {
// however, this way it does not work, and "number" can still be undefined
console.log(number)
})
I am not sure why TypeScript is not inferring the results in the second way.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:19 (3 by maintainers)
Top Results From Across the Web
Documentation - Type Compatibility - TypeScript
Type compatibility in TypeScript is based on structural subtyping. ... provides three parameters to the callback function: the array element, its index, ...
Read more >typescript - Filter an array with typegaurd doesn't produce an ...
The issue here is that you have a value of union type (Section | Link)[] | [] and you're trying to call its...
Read more >Node.js v19.3.0 Documentation
Welcome to the official API reference documentation for Node.js! Node.js is a JavaScript ... The feature is not subject to semantic versioning rules....
Read more >Type Guard - TypeScript Deep Dive - Gitbook
Type Guards allow you to narrow down the type of an object within a ... where TypeScript realizes that a particular function does...
Read more >TypeScript errors and how to fix them
error TS1055: Type ' AxiosPromise ' is not a valid async function return type in ES5/ES3 because it does not refer to a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, the issue is described here: https://github.com/microsoft/TypeScript/issues/10734 and here: https://github.com/microsoft/TypeScript/issues/16069. Would it be sensible to add an exception for functions with type guards (either by default or through an option)?
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
eslint-plugin-functional
; good work!