question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`no-array-callback-reference` rule is not compatible with TypeScript type guards

See original GitHub issue
function 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
oss6commented, Jun 28, 2020

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)?

1reaction
Vagescommented, Sep 11, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found