Skip to content

Use readonly in params#59

Open
mchevestrier wants to merge 1 commit into
mapbox:mainfrom
mchevestrier:readonly-params
Open

Use readonly in params#59
mchevestrier wants to merge 1 commit into
mapbox:mainfrom
mchevestrier:readonly-params

Conversation

@mchevestrier
Copy link
Copy Markdown

@mchevestrier mchevestrier commented Apr 8, 2026

Since array arguments are not mutated, we can add readonly to array parameter types.

This way we can directly pass readonly arrays to CheapRuler methods:

const ruler = new CheapRuler(42, 'miles');

// Readonly arrays
const a = [1, 2] as const;
const b = [3, 4] as const;

ruler.distance(a, b); // Ok!

Otherwise, we have to needlessly copy the arrays to avoid TypeScript errors:

// @ts-expect-error - Argument of type 'readonly [1, 2]' is not assignable to parameter of type '[number, number]'. The type 'readonly [1, 2]' is 'readonly' and cannot be assigned to the mutable type '[number, number]'.
ruler.distance(a, b);

// or copy the arrays to avoid errors:
ruler.distance([...a], [...b]);

@mchevestrier mchevestrier marked this pull request as ready for review April 8, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant