prefer-type-fest-array-length
Require TypeFest ArrayLength<T> over array and tuple T["length"] type queries.
Targeted pattern scopeโ
This is a type-aware rule. It targets indexed-access type queries whose object type resolves to an array or tuple.
What this rule reportsโ
T["length"]whenTis an array or tuple type.
Why this rule existsโ
ArrayLength<T> is the dedicated TypeFest helper for array and tuple length extraction. Using it makes intent explicit and aligns with the canonical TypeFest helper name.
โ Incorrectโ
type StepCount = EventSteps["length"];
โ Correctโ
import type { ArrayLength } from "type-fest";
type StepCount = ArrayLength<EventSteps>;
Behavior and migration notesโ
- This rule requires type information.
- It only reports
T["length"]whenTresolves to an array-like type. - It does not report non-array property lookups like
User["length"].
Additional examplesโ
โ Incorrect โ Additional exampleโ
type TupleLength = readonly [1, 2, 3]["length"];
โ Correct โ Additional exampleโ
import type { ArrayLength } from "type-fest";
type TupleLength = ArrayLength<readonly [1, 2, 3]>;
โ Correct โ Non-targeted usageโ
interface User {
readonly length: number;
readonly name: string;
}
type UserLength = User["length"];
ESLint flat config exampleโ
import typefest from "eslint-plugin-typefest";
export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-array-length": "error",
},
},
];
When not to use itโ
Disable this rule if you prefer native indexed-access syntax for array length queries or if your project does not use type-aware linting.
Package documentationโ
TypeFest package documentation:
Source file: source/array-length.d.ts
/**
Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.
*/
export type ArrayLength<T extends readonly unknown[]> = T['length'];
Rule catalog ID: R078