prefer-ts-extras-array-first
Require arrayFirst from ts-extras over direct array[0] access.
Targeted pattern scopeโ
This rule only matches direct first-element index access (receiver[0]) where the receiver expression can be reused unchanged in arrayFirst(receiver).
- Direct first-element access using index form (
array[0]).
Syntactically similar alternatives are intentionally out of scope unless they preserve the same AST shape.
What this rule reportsโ
This rule reports direct receiver[0] access sites that can be safely replaced with arrayFirst(receiver).
- Direct first-element access using index form (
array[0]).
Why this rule existsโ
arrayFirst makes first-element access explicit and keeps tuple/readonly inference consistent with the rest of the ts-extras helper set.
- First-element lookups are easier to search and standardize.
- Tuple-aware access patterns are consistent in shared utilities.
- Teams avoid mixing helper-based and index-based first-item access.
โ Incorrectโ
const first = values[0];
โ Correctโ
const first = arrayFirst(values);
Behavior and migration notesโ
- Runtime behavior matches
array[0]access. - Empty arrays still yield
undefined. - This rule targets index access; optional chaining around access (
array?.[0]) should be reviewed manually during migration.
Additional examplesโ
โ Incorrect โ Additional exampleโ
const first = rows[0];
โ Correct โ Additional exampleโ
const first = arrayFirst(rows);
โ Correct โ Repository-wide usageโ
const header = arrayFirst(headers);
ESLint flat config exampleโ
import typefest from "eslint-plugin-typefest";
export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-array-first": "error",
},
},
];
When not to use itโ
Disable this rule if direct index access is required in performance-sensitive hotspots.
Package documentationโ
ts-extras package documentation:
Source file: source/array-first.ts
/**
Return the first item of an array with stronger typing for tuples.
This mirrors getting `array[0]` but with better type safety and handling for empty arrays.
@example
```
import {arrayFirst} from 'ts-extras';
const tuple = ['abc', 123, true] as const;
const first = arrayFirst(tuple);
//=> 'abc'
// ^? 'abc'
const array = ['a', 'b', 'c'];
const maybeFirst = arrayFirst(array);
//=> 'a'
// ^? string | undefined
// Empty arrays
const empty: string[] = [];
const noFirst = arrayFirst(empty);
//=> undefined
// ^? string | undefined
```
@category Improved builtin
*/
Rule catalog ID: R006