8000 Rule proposal: disallow unused return types · Issue #6442 · typescript-eslint/typescript-eslint · GitHub 646D
[go: up one dir, main page]

Skip to content
Rule proposal: disallow unused return types #6442
Open
@KhafraDev

Description

@KhafraDev

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Paired with explicit-function-return-types, it's possible to include extraneous return types that are never used. This can happen when refactoring code meaning that some code branches might become impossible to reach.

For example you might have this function:

function random (param?: string): string | null {
  if (typeof param !== 'string') return null
  
  return 'has param'
}

where the return types are correct, but then it's refactored to

// note that param is no longer optional but the return type is the same union
function random (param: string): string | null {
  return 'has param'
}

and let's assume it's being used in a codebase before param is required:

const rand = random(param)

if (rand === null) {
  throw new Error('...')
}

since the return type is unchanged, the null check branch might not be removed during refactoring, and neither the linter or tsc will complain about it.

Fail Cases

function test (): string | null {
  return 'string'
}

class A {
  static test (): string | null {
    return 'string'
  }

  test (): string | null {
    return 'string'
  }
}

Pass Cases

function test (): string {
  return 'string'
}

class A {
  static test (): string | null {
    if (Math.random() < 0.5) return null

    return 'string'
  }

  test (): string {
    return 'string'
  }
}

Additional Info

It's totally possible I missed an issue that matched this proposal, but I couldn't find any.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0