-
Notifications
You must be signed in to change notification settings - Fork 643
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
Take a readonly array when possible #1314
Comments
I usually use this pattern in ts with enumerations enum Color {
Red = "red",
Green = "green"
}
const colorType = types.enumeration<Color>(Object.values(Color))
const myModel = types.model({
color: colorType,
}) |
But that being said, yeah, it should take a ReadonlyArray to not leave old ts users out |
When initializing a model with arrays, it also doesn't accept read-only arrays. Is that because the array instance is reused? |
I think that i just an omission in the typings and should be safe.
…On Wed, Apr 22, 2020 at 6:03 PM Michael Best ***@***.***> wrote:
When initializing a model with arrays, it also doesn't accept read-only
arrays. Is that because the array instance is reused?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1314 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBFRWIO732NYX6UZ67TRN4PNTANCNFSM4HW7I4QA>
.
|
Here's another (AFAIK most DRY) way to use your readonly arrays for union types and as input for an enumeration type, just use array spread:
|
New PR created to fix the types for this: #2059. |
Bug report
Sandbox link or minimal reproduction code
Describe the expected behavior
The above code should compile
Describe the observed behavior
Line 10 shows the following error:
This is due to
enumeration
accepting T[], and not aReadonlyArray<T>
/readonly T[]
(latter as of TS 3.4).This behavior is not just for this case, but also in other places in MST (e.g.
types.union
.My suggestion is to accept the minimal necessary interface for arrays when possible. This should cover most, if not all cases, since they are usually not directly used anyway, at least for types. This is more important with TypeScript 3.4+ since it's now much less of a hassle to make an array a
ReadonlyArray
, especially withas const
, but also with thereadonly
modifier added to more use-uses (e.g. function parameters).If this is handled, upgrading to TypeScript 3.4 (or greater) should be considered, since it makes the changes easier to type. e.g.:
At the cost of supporting only TS 3.4+ (maybe we can transpile these to
ReadonlyArray
in the output type declarations?)The text was updated successfully, but these errors were encountered: