Skip to content

Types rules

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.

Remove ? when the property must be present, or remove undefined from the type.

type User = { name?: string | undefined }; reports optional. Write name?: string.

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.

Remove the member that adds nothing, or write the type that the intersection should have.

type User = Person & {}; reports empty. Write type User = Person;.

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.

Add readonly to the static property, or make mutable state private.

class Clock { static readonly interval = 1000; } protects the public shared value.

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.

Declare a type alias for the composite type and use its name in each place.

Three parameters typed "small" | "medium" | "large" report alias at the first. Declare type Size = "small" | "medium" | "large";.

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.

Declare the result as a type predicate, such as function isFish(value: Pet): value is Fish.

function isFish(pet: Pet) { return (pet as Fish).swim !== undefined; } reports guard. Add : pet is Fish.

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.

Use the original type. Keep aliases that define a union, a shape, or concrete generic arguments.

type Label = string; adds no constraint. Use string at its call sites.