-
Notifications
You must be signed in to change notification settings - Fork 2
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
PrincipledArray #7
Comments
Such a type would look something like this: // https://github.com/agiledigital/readonly-types/issues/518
export type VeryImmutableArray<T> = ImmutableShallow<
OmitStrict<ImmutableArray<T>, "map">
> & {
readonly map: <U>(
callbackfn: (value: T, index: number, array: ImmutableArray<T>) => U,
thisArg?: any
) => ImmutableArray<U>;
}; Usage: const foo: VeryImmutableArray<string> = [""] as const;
const bar = foo.map((a) => a.toUpperCase());
// Doesn't compile 🚀!
bar[0] = ""; |
A complication here is that the methods available on the I wonder if, at the type level, we can dynamically detect if those methods are present and only provide the principled versions if so. Alternatively we do the chauvinist thing and assume they're available in every runtime, regardless of which libs are configured. |
Although, we've already committed an amount of that chauvinism by including this:
in tsconfig 🤔 |
More things to consider changing if we're going to lean into "principled":
|
I have published an early version of this type in version It:
I may remove Note that these changes render the That means it'll have "worse" ergonomics when interacting with code (functions, libraries, ...) that expects built in readonly or mutable arrays. This is known / by design, so don't complain about it. If you want something that is "as readonly as possible" while remaining type compatible with those built-in types, |
Another principled decision: Don't expose the versions of This is equivalent to the situation with |
Released in 4.4.0:
|
It's too easy to "escape" into mutable arrays from a ReadonlyArray (e.g.
map
,filter
return mutable arrays). Let's provide a principled, immutable array type that doesn't make this so easy.The text was updated successfully, but these errors were encountered: