unbounded-spread
A list of unknown length spread into a built-in that takes its elements as arguments: Math.max(...values), target.push(...extra), String.fromCharCode(...codes). On by default, as a warning, like the other rules about lists of any length (bounded-concurrency, no-accumulating-copy).
Why: a spread argument puts every element on the stack as an argument of its own. Past roughly 100,000 of them (the number depends on the engine and on how much stack is left) the call throws RangeError: Maximum call stack size exceeded. Tests use short lists, production gets the long one. Math.max and Math.min have a second trap: an empty list gives -Infinity / Infinity, a number that looks like an answer and flows on into arithmetic.
✅ Accepted: maxOf takes the list, and says when there is no largest
✓ Accepted0 diagnostics
ts
import { maxOf } from "@evobug_com/safe-ts-std/array";
import type { Option } from "@evobug_com/safe-ts-std/result";
export function highestScore(scores: readonly number[]): Option<number> {
return maxOf(scores);
}
export function withExtra(base: readonly number[], extra: readonly number[]): readonly number[] {
const out = [...base];
for (const item of extra) out.push(item);
return out;
}
export function text(codes: readonly number[]): string {
return codes.map((code) => String.fromCharCode(code)).join("");
}ts
import { maxOf } from "@evobug_com/safe-ts-std/array";
import type { Option } from "@evobug_com/safe-ts-std/result";
export function highestScore(scores: readonly number[]): Option<number> {
return maxOf(scores);
}
export function withExtra(base: readonly number[], extra: readonly number[]): readonly number[] {
const out = [...base];
for (const item of extra) out.push(item);
return out;
}
export function text(codes: readonly number[]): string {
{ const __la1 = codes; const __lr1: any[] = new Array(__la1.length); for (let __li1 = 0, __ln1 = __la1.length; __li1 < __ln1; __li1++) { if (!(__li1 in __la1)) continue; const __le1 = __la1[__li1]!; { let code = __le1; __lr1[__li1] = (String.fromCharCode(code)); }} return __lr1.join(""); }
}✕ Rejectedunbounded-spread
ts
export function highestScore(scores: readonly number[]): number {
return Math.max(...scores);
}✕ Rejectedunbounded-spread
ts
export function joined(parts: readonly (readonly number[])[]): readonly number[] {
const out: number[] = [];
for (const part of parts) out.push(...part);
return out;
}The built-ins, and what to write instead
| Call | Instead |
|---|---|
Math.max(...xs), Math.min(...xs) | maxOf(xs), minOf(xs) from @evobug_com/safe-ts-std/array: Option<number>, undefined for [], otherwise what Math.max / Math.min gives |
Math.hypot(...xs) | a loop summing the squares, then Math.sqrt |
String.fromCharCode(...codes), String.fromCodePoint(...points) | codes.map((c) => String.fromCharCode(c)).join(""); for UTF-8 bytes, new TextDecoder().decode(bytes) |
target.push(...xs) | for (const x of xs) target.push(x), or a new array [...target, ...xs] |
target.unshift(...xs), target.splice(i, n, ...xs) | a new array: [...xs, ...target], [...target.slice(0, i), ...xs, ...target.slice(i + n)] |
a.concat(...lists) | [...a, ...lists.flat()] for arrays, a + parts.join("") for strings |
Array.of(...xs), Uint8Array.of(...xs) (any typed array) | Array.from(xs), Uint8Array.from(xs) |
Object.assign(target, ...sources) | for (const s of sources) Object.assign(target, s) |
node.append(...nodes), prepend, replaceChildren | append to a DocumentFragment in a loop, then node.append(fragment) |
.call(thisArg, ...xs) and .apply(thisArg, xs) of these (Math.max.apply(null, xs), Array.prototype.push.apply(a, b)) and Reflect.apply(Math.max, undefined, xs) are reported the same way. A spread inside an array literal ([...a, ...b]) has no such limit: it iterates.
Not reported
✅ Also accepted: lengths that are known, or already fit as arguments
✓ Accepted0 diagnostics
ts
const SIZES = [3, 4] as const;
export function fine(a: number, b: number, pair: readonly [number, number], ...rest: readonly number[]): number {
const all = [...rest, ...pair];
all.push(a, b);
const head = rest.slice(0, 3);
return Math.max(a, b) + Math.max(...[1, 2, 3]) + Math.min(...pair) + Math.hypot(...SIZES) + Math.max(...rest) + Math.max(...head);
}✅ Also accepted: a tuple type named by an alias, of this file or imported
✓ Accepted0 diagnostics
ts
type Vec3 = readonly [x: number, y: number, z: number];
type Size = readonly [width: number, height: number];
export function length(v: Vec3, size: Size): number {
return Math.hypot(...v) + Math.max(...size);
}❌ Still reported: a tuple with a rest element has no fixed length
✕ Rejectedunbounded-spread
ts
type AtLeastOne = readonly [number, ...number[]];
export function largest(values: AtLeastOne): number {
return Math.max(...values);
}✅ Also accepted: a method of the receiver's own type
✓ Accepted0 diagnostics
ts
type Router = { readonly push: (...segments: readonly string[]) => void };
export function open(router: Router, segments: readonly string[]): void {
router.push(...segments);
}- an array literal (whose own spreads are known), a string literal, a value declared with a tuple type without a rest element or
as const, andmap,filter,slice,toSorted,toReversedorwithof one of those (...[0, 1, 2].map(f)),a ?? bandc ? a : bof two of them,v!, andslice(0, 10)with two literal bounds. The tuple type can be written in place (readonly [number, number]), with labelled and optional elements ([from: number, to?: number]), asReadonly<...>of one, as a union of them (| undefinedaside), or through a type alias: one of the same file, or one imported from a project file (also re-exported, or an alias of another alias). A rest element ([number, ...number[]]) has no fixed length and stays reported, as does an alias of a type parameter (type Wrapped<T> = T); - a
constwhose value is one of those (const head = values.slice(0, 3)), as long as nothing can make it longer: nopush,unshiftorspliceon it, no write to itslengthor an index, and it isn't handed to anything (an argument, another binding, a return value) but a spread, afor ofor a read of its members and elements; - a rest parameter (
...args) andarguments: their elements came in as the arguments of one call, so they fit as the arguments of another; - a spread into a function of the project (
f(...args)), andnew(new Date(...parts)): only the built-ins above are known to take whole lists; push,unshift,splice,concat,append,prependorreplaceChildrencalled on a receiver whose declared type is a class, interface or type alias of the project that declares that member itself (router.push(...names)withrouter: Router). The receiver is a parameter, a variable or a fieldthis.xdeclared with that type (alsoReadonly<T>,Mut<T>,T | undefined, or one part of an intersection) or made withnew C(...), orthisin the class; the type is declared in the same file or imported from a project file (not a package's type, nor one from a.d.tsfile).
What the rule can't see
- The receiver's type, where it isn't written on the receiver as above: there is no type checker, so
getRouter().push(...names), a receiver whose type is inferred from anything butnew C(...), a type parameter (<T extends Router>), or a member the type inherits (extends) rather than declares is taken for the built-in. Annotate the receiver, or give the method a list parameter (pushAll(items)), which is the better signature anyway. - A list whose length is bounded by the code around it (
values.slice(0, n), a list built from three items withpush, alet): only the forms above count as known. Write the bound as a tuple type, or use the replacement, which costs nothing for a short list. - That a list of known length isn't empty:
Math.max(...values.slice(0, 16))is not reported, and still gives-Infinityfor an emptyvalues.maxOfsays so withundefined. - A spread into
console.logor a library's variadic function.