A tiny and type-safe TypeScript utility to create and work with fixed-length arrays (tuples).
npm install fixed-len-array
A tiny and type-safe TypeScript utility to create and work with fixed-length arrays (tuples). Automatically trims or pads input arrays at runtime, while offering compile-time safety using TypeScript's type system.
- โ
Enforces fixed-length array types (tuples)
- ๐ง Type-safe construction
- ๐ง Automatically trims or pads at runtime
- ๐ Pads with a custom value or falls back to null if not provided
- ๐ฆ Tiny, no dependencies
- ๐ Works in both Node.js and browser environments (ESM only)
``sh`
npm install fixed-len-array
`ts
import { toFixedLengthArray, FixedLengthArray } from "fixed-len-array";
// Pads to a fixed length of 3 with custom default
const vec3 = toFixedLengthArray([1], 3, 0);
// Result: [1, 0, 0]
// If no default is provided, pads with null
const paddedWithNull = toFixedLengthArray([1], 3);
// Result: [1, null, null]
// Trims if input is longer
const trimmed = toFixedLengthArray([1, 2, 3, 4, 5], 3, 0);
// Result: [1, 2, 3]
// Full type support
type Vec3 = FixedLengthArray<3, number>;
`
Creates a fixed-length array by trimming or padding the input. If defaultValuenull
is omitted, will be used for padding.
A recursive type that represents a tuple of length N, with all elements ofT
type .
We welcome contributions! Please see our Contributing Guide
for details.
`bashClone repository
git clone https://github.com/ptprashanttripathi/fixed-len-array.git
cd fixed-len-array
This project is MIT licensed.
---