Skip to content

Rule proposal: Detect useless default in function parameters and object destructuring #6106

@RunDevelopment

Description

@RunDevelopment

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

Default parameters and default values are only used if the parameter or property is undefined (either because its value is undefined or because it is missing). So if we know that a function parameter or property can never be undefined, then we also know that the default is never actually used. This can be used to detect useless defaults.

Fail Cases

// React props destructuring
function Bar({ foo = "" }: { foo: string }) { /* ... */ }

// destructuring assignment
const { foo = "" } = { foo: "bar" }

// default parameter
[1, 2, 3].map((a = 42) => a + 1)

Pass Cases

// React props destructuring
function Bar({ foo }: { foo: string }) { /* ... */ }
function Bar({ foo = "" }: { foo?: string }) { /* ... */ }

// destructuring assignment
const { foo } = { foo: "bar" }

// default parameter
[1, 2, 3].map((a) => a + 1)
[1, 2, 3, undefined].map((a = 42) => a + 1)

Additional Info

This rule requires strict null checks to be enabled.

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