aegir icon indicating copy to clipboard operation
aegir copied to clipboard

feat!: enable ts noImplicitReturns

Open wemeetagain opened this issue 3 years ago • 3 comments

As discussed offline, this feature helps avoid issues like https://github.com/libp2p/js-libp2p-tcp/issues/185

wemeetagain avatar Aug 02 '22 19:08 wemeetagain

Just tried this out locally. Given this function:

function foo (n: number): string | undefined {
  if (n > 10) {
    return 'ok'
  }
}

I get a ts build error:

 error TS7030: Not all code paths return a value.

1   function foo (n: number): string | undefined {

to make the error go away I have to do:

function foo (n: number): string | undefined {
  if (n > 10) {
    return 'ok'
  }

  return undefined   //  <- add this line
}

Do we want this? Seems like it adds unnecessary boilerplate.

If instead we switch @typescript-eslint/explicit-function-return-type to 'error' in eslint-ipfs-config, building succeeds but linting fails, and all we have to do to appease the linter is add the return type, not the extra return undefined.

What do you think?

achingbrain avatar Aug 10 '22 08:08 achingbrain

The ts config change in this PR doesn't seem to actually require return types either, only when there's a branch in the function in question that affects the return type.

The eslint rule OTOH is a little less forgiving and requires return types all the time, regardless of the function structure.

By which I mean this builds fine with noImplicitReturns: true but needs "@typescript-eslint/explicit-function-return-type": "error" to fail linting:

function foo () {
  return 'hello'
}

achingbrain avatar Aug 10 '22 08:08 achingbrain

See: https://github.com/ipfs/eslint-config-ipfs/pull/114

achingbrain avatar Aug 10 '22 09:08 achingbrain

Closing in favor of https://github.com/ipfs/eslint-config-ipfs/pull/114, which will get dependency updated in aegir by dependabot.

BigLep avatar Sep 02 '22 14:09 BigLep