Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapArray is typed in a way that doesn't work with readonly source arrays #274

Open
diegovilar opened this issue Feb 13, 2024 · 0 comments · May be fixed by #275
Open

mapArray is typed in a way that doesn't work with readonly source arrays #274

diegovilar opened this issue Feb 13, 2024 · 0 comments · May be fixed by #275

Comments

@diegovilar
Copy link
Contributor

Because mapArray's implementation expects a mutable array type

export const mapArray = <T, R>(mapFn: (item: T, index: number) => R) =>
  map((array: T[]) => array.map((item, index) => mapFn(item, index)));

using it with a readonly source array will make TypeScript complain with TS2345:

const source: readonly number[] = [1, 2, 3];

const source$ = of(source).pipe(
  mapArray((item) => item * 2)
  // ^ The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.ts(2345)
);

I would argue that any function consuming an array should most likely type the argument as readonly so both mutable and readonly arrays are accepted. Except, of course, if said function is supposed to actually modify the consumed array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant