Skip to main content

prefer-type-fest-extract-rest-element

Require TypeFest ExtractRestElement<T> over SplitOnRestElement<T>[1][number] rest-element extraction.

Targeted pattern scopeโ€‹

This rule targets direct and namespace-qualified references to SplitOnRestElement<T>[1][number] imported from type-fest.

What this rule reportsโ€‹

  • SplitOnRestElement<T>[1][number]

Why this rule existsโ€‹

ExtractRestElement is the public TypeFest helper for extracting the rest element type from a tuple or array. It communicates that intent directly instead of exposing the internal tuple returned by SplitOnRestElement.

โŒ Incorrectโ€‹

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

type Rest = SplitOnRestElement<[number, ...string[], boolean]>[1][number];

โœ… Correctโ€‹

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

type Rest = ExtractRestElement<[number, ...string[], boolean]>;

Behavior and migration notesโ€‹

  • This rule only reports SplitOnRestElement imported from type-fest.
  • It only reports the exact rest-element extraction segment, [1][number].
  • It leaves SplitOnRestElement<T>[0], SplitOnRestElement<T>[1], and SplitOnRestElement<T>[2] alone.
  • Namespace-qualified type-fest references are reported too.

Additional examplesโ€‹

โŒ Incorrect โ€” Namespace importโ€‹

import type * as TypeFest from "type-fest";

type Rest = TypeFest.SplitOnRestElement<Tuple>[1][number];

โœ… Correct โ€” Namespace importโ€‹

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

type Rest = ExtractRestElement<Tuple>;

โœ… Correct โ€” Non-targeted segmentโ€‹

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

type Prefix = SplitOnRestElement<Tuple>[0];

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if your codebase intentionally works with the raw tuple returned by SplitOnRestElement.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/extract-rest-element.d.ts

/**
Extract the rest element type from an array.
*/
export type ExtractRestElement<T extends UnknownArray> =
SplitOnRestElement<T>[1][number];

Rule catalog ID: R115

Further readingโ€‹

Adoption resourcesโ€‹