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

add array-map for keeping tuple length when using .map #136

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tjenkinson
Copy link

@tjenkinson tjenkinson commented Apr 9, 2023

When you're using Array.map with a tuple, the length is lost. This means you loose the guard against accessing an item out of bounds.

// BEFORE
const tuple = [1, 2, 3] as const;
const mapped = tuple.map((a) => a + 1);
// oops. There's no 4th element, but no error
console.log(mapped[3]);

With array-map enabled, this code will now error:

// AFTER
import "@total-typescript/ts-reset/array-map";
const tuple = [1, 2, 3] as const;
const mapped = tuple.map((a) => a + 1);
// Tuple type 'readonly [number, number, number]' of length '3' has no element at index '3'.
console.log(mapped[3]);

readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
Co-authored-by: Sindre Bøyum <boyum@users.noreply.github.com>
@Eshanatnight
Copy link

// BEFORE
const tuple = [1, 2, 3] as const;
const mapped = tuple.map((a) => a + 1);
// oops. There's no 4th element, but no error
console.log(tuple[3]);
// AFTER
import "@total-typescript/ts-reset/array-map";
const tuple = [1, 2, 3] as const;
const mapped = tuple.map((a) => a + 1);
// Tuple type 'readonly [number, number, number]' of length '3' has no element at index '3'.
console.log(tuple[3]);

shouldnt the console.log be console.log(mapped[3]);

@tjenkinson
Copy link
Author

Oops. Yep good spot thanks fixed now

@Eshanatnight
Copy link

Eshanatnight commented May 21, 2023

It was just me being pedantic. 🤗

map<U>(
callbackfn: (value: T, index: number, array: readonly T[]) => U,
thisArg?: any,
): { [K in keyof this]: U };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
): { [K in keyof this]: U };
): { readonly [K in keyof this]: U };

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @ahrjarrett what difference does this make? Could we test it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind, if this doesn't seem to make a difference, that only happens to be true for the set of inputs we've tried it with.

Fixing the output of a readonly array's map operation to be readonly seems like a reasonable thing to do. It creates a cohesive API, its semantics are unambiguous, and it eliminates the possibility that one of the property modifiers might change out from under us.

Probably a moot point anyway, since I don't know if this addition is in keeping with the design goals of ts-reset. That said, I'm personally all for preserving structure like this where possible, so I went ahead and made the suggestion :)

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.

None yet

4 participants