Skip to content

a-tarasyuk/eslint-plugin-redundant-undefined

Repository files navigation

eslint-plugin-redundant-undefined

Forbids optional parameters to include an explicit undefined in their type and requires to use undefined in optional properties.

GitHub license npm version GitHub Workflow Status (with branch) Coverage Status npm downloads

Installation

$ npm i eslint-plugin-redundant-undefined @typescript-eslint/parser --save-dev

Usage

Add redundant-undefined to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["redundant-undefined"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "redundant-undefined/redundant-undefined": "error"
  }
}

Rule Details

  • Avoid explicitly specifying undefined as a type for a parameter/property which is already optional

Options

  • followExactOptionalPropertyTypes - Requires explicitly specifying undefined as a type for a parameter which is already optional., this provides the correct semantics for people who have exactOptionalPropertyType: true

Examples

Examples of incorrect code:

function f(s?: undefined | string): void {}

function f(s?: number | undefined | string): void {}

interface I {
  a?: string | undefined;
}

class C {
  a?: string | undedined;
}

Examples of correct code:

function f(s?: string): void {}

interface I {
  a?: string;
}

interface I {
  a?: any;
}

class C {
  a?: string;
}

Examples of incorrect code for the { "followExactOptionalPropertyTypes": true }:

interface I {
  p?: string;
}

class C {
  private p?: number;
}

abstract class C {
  abstract p?: string;
}

Examples of correct code for the { "followExactOptionalPropertyTypes": true }:

interface I {
  p?: string | undefined;
}

interface I {
  p?: any;
}

class C {
  private p?: number | undefined;
}

abstract class C {
  abstract p?: string | undefined;
}

License and Copyright

This software is released under the terms of the MIT license.