JavaScript

JavaScript

Made by DeepSource

Optional property in interface has an 'undefined' type JS-T1001

Type check
Minor
Autofix

Optional property syntax (?) implicitly adds an 'undefined' type to the property.

Explicitly having an 'undefined' type in the union of an optional property is redundant and should be avoided to improve the readability of the code. Using '?' and 'undefined' together can lead to unexpected behviour in the code. '?' will allow you to skip a property even if 'undefined' is mentioned as one of the types. If 'undefined' needs to be enforced as one of the types of that property, then the optional property syntax should be removed from the annotation.

Bad Practice

interface Foo {
  bar: string;
  quz?: string | undefined; // Here '| undefined is not necessary since '?' is already used on the property'
}

Recommended

interface Foo {
  bar: string;
  quz?: string;
}