Skip to main content

prefer-type-fest-arrayable

Require TypeFest Arrayable<T> over T | T[] and T | Array<T> unions.

Targeted pattern scopeโ€‹

This rule only matches the canonical value-or-array union spellings that TypeFest replaces with Arrayable<T>.

  • T | T[]
  • T | Array<T>

Near-miss unions (extra members or mismatched element types) are excluded unless they are exact canonical forms.

What this rule reportsโ€‹

This rule reports canonical value-or-array unions that should be rewritten as Arrayable<T>.

  • T | T[]
  • T | Array<T>

Why this rule existsโ€‹

Arrayable<T> is clearer and more consistent than repeating union patterns. It also aligns code with TypeFest utility conventions used by other rules in this plugin.

โŒ Incorrectโ€‹

type Input = string | string[];

โœ… Correctโ€‹

type Input = Arrayable<string>;

Behavior and migration notesโ€‹

  • Arrayable<T> is the canonical form for value-or-array contracts.
  • It replaces both T | T[] and T | Array<T> spelling variants.
  • Keep this alias in public callback and option signatures to reduce repeated union boilerplate.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

type Input = number | Array<number>; // Union repeated inline across modules

โœ… Correct โ€” Additional exampleโ€‹

type Input = Arrayable<number>;

โœ… Correct โ€” Repository-wide usageโ€‹

type QueryParam = Arrayable<string>;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if existing public type names must remain unchanged for compatibility.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/arrayable.d.ts

/**
* Create a type that represents either the value or an array of the value.
*
* @category Array
*
* @example
* ```
* import type {Arrayable} from 'type-fest';
*
* function bundle(input: string, output: Arrayable<string>) {
* const outputList = Array.isArray(output) ? output : [output];
*
* // โ€ฆ
*
* for (const output of outputList) {
* console.log(`write ${input} to: ${output}`);
* }
* }
*
* bundle('src/index.js', 'dist/index.js');
* bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']);
* ```
*
* @see {@link Promisable}
*/

Rule catalog ID: R036

Further readingโ€‹

Adoption resourcesโ€‹