Skip to main content

prefer-type-fest-absolute

Require TypeFest Absolute over common Abs or AbsoluteValue aliases.

Targeted pattern scopeโ€‹

This rule reports Abs<N> and AbsoluteValue<N> type references and prefers the canonical Absolute<N> from type-fest for numeric absolute-value type computations.

What this rule reportsโ€‹

  • Type references named Abs.
  • Type references named AbsoluteValue.

Detection boundariesโ€‹

  • โœ… Reports direct Abs<N> and AbsoluteValue<N> type references.
  • โœ… Autofixes by renaming the identifier to Absolute and inserting a type-fest import when absent.
  • โŒ Does not auto-fix where the Absolute identifier is shadowed by a type parameter in scope.

Why this rule existsโ€‹

Absolute<N> is the canonical TypeFest utility for computing the absolute value of a numeric literal type.

Using a consistent name across a codebase avoids confusion between locally-defined Abs helpers and the standard TypeFest utility. A single canonical name also makes the intent immediately readable by contributors familiar with type-fest.

โŒ Incorrectโ€‹

type Result = Abs<-5>;
type Other = AbsoluteValue<-100>;

โœ… Correctโ€‹

import type { Absolute } from "type-fest";

type Result = Absolute<-5>;
type Other = Absolute<-100>;

Behavior and migration notesโ€‹

  • Absolute<N> strips the leading minus sign from a negative numeric literal type, producing the non-negative form.
  • Locally-defined Abs or AbsoluteValue helpers may have different semantics (e.g., different constraints). Verify the behavior is equivalent before applying the autofix.
  • The autofix is safe to apply when the local alias is a mere re-export or thin wrapper over Absolute.

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-absolute": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your codebase intentionally standardizes Abs or AbsoluteValue naming, or if your local alias provides additional constraints not present in the TypeFest utility.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/absolute.d.ts

/**
Returns the absolute value of a given integer type.

@example
```
import type {Absolute} from 'type-fest';

type SomeValue = Absolute<-1>;
//=> 1
```

@category Numeric
*/

Rule catalog ID: R097

Further readingโ€‹

Adoption resourcesโ€‹