Forbids optional parameters to include an explicit
undefined
in their type and requires to useundefined
in optional properties.
$ npm i eslint-plugin-redundant-undefined @typescript-eslint/parser --save-dev
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"
}
}
- Avoid explicitly specifying
undefined
as a type for a parameter/property which is already optional
followExactOptionalPropertyTypes
- Requires explicitly specifyingundefined
as a type for a parameter which is already optional., this provides the correct semantics for people who haveexactOptionalPropertyType: true
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;
}
This software is released under the terms of the MIT license.