Types rules
no-redundant-optional
Section titled “no-redundant-optional”Reports an optional property whose type also includes undefined, written in a union or, with type information, through a type alias, unless the project sets exactOptionalPropertyTypes.
On in the recommended and all configs.
Without exactOptionalPropertyTypes, ? already adds undefined to the type, so writing both says the same thing twice.
What to do
Section titled “What to do”Remove ? when the property must be present, or remove undefined from the type.
Example
Section titled “Example”type User = { name?: string | undefined }; reports optional. Write name?: string.
no-useless-intersection
Section titled “no-useless-intersection”Reports an intersection with any or never, and an intersection member that has no members, such as null, undefined, void, or {}.
On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.
Such an intersection is always any or never, or one of its members adds nothing, so it does not say what the reader expects.
What to do
Section titled “What to do”Remove the member that adds nothing, or write the type that the intersection should have.
Example
Section titled “Example”type User = Person & {}; reports empty. Write type User = Person;.
prefer-readonly-static
Section titled “prefer-readonly-static”Requires public static TypeScript properties to be readonly.
On in the all config.
A writable public static property lets any caller change state shared by every class instance.
What to do
Section titled “What to do”Add readonly to the static property, or make mutable state private.
Example
Section titled “Example”class Clock { static readonly interval = 1000; } protects the public shared value.
prefer-type-alias
Section titled “prefer-type-alias”Reports a union or intersection of three or more members that a file writes out three or more times, in any member order, outside type alias declarations. A union of one type with null or undefined passes.
On in the all config.
A long composite type repeated in place hides that the places share one concept, and each copy must change together.
What to do
Section titled “What to do”Declare a type alias for the composite type and use its name in each place.
Example
Section titled “Example”Three parameters typed "small" | "medium" | "large" report alias at the first. Declare type Size = "small" | "medium" | "large";.
prefer-type-guard
Section titled “prefer-type-guard”Reports a function of one parameter without a type predicate whose only statement returns a check that a member of a cast value exists, such as (value as Fish).swim !== undefined.
On in the all config.
The check narrows nothing for the caller, who has to cast the value again after a true result.
What to do
Section titled “What to do”Declare the result as a type predicate, such as function isFish(value: Pet): value is Fish.
Example
Section titled “Example”function isFish(pet: Pet) { return (pet as Fish).swim !== undefined; } reports guard. Add : pet is Fish.
redundant-type-aliases
Section titled “redundant-type-aliases”Reports aliases that only rename a primitive or another type.
On in the recommended and all configs.
A second name hides the original type without adding a constraint.
What to do
Section titled “What to do”Use the original type. Keep aliases that define a union, a shape, or concrete generic arguments.
Example
Section titled “Example”type Label = string; adds no constraint. Use string at its call sites.