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>andAbsoluteValue<N>type references. - โ
Autofixes by renaming the identifier to
Absoluteand inserting atype-festimport when absent. - โ Does not auto-fix where the
Absoluteidentifier 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
AbsorAbsoluteValuehelpers 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