Skip to content

Commit 92ee2a1

Browse files
authored
fix(types): Accept readonly arrays as input for array type (#1936 by @adamkovalsky)
1 parent dafdb9c commit 92ee2a1

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/mobx-state-tree/__tests__/core/array.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,10 @@ test("assigning filtered instances works", () => {
560560
expect(done.every((t) => !isAlive(t))).toBe(true)
561561
expect(notDone.every((t) => isAlive(t))).toBe(true)
562562
})
563+
564+
test("#1676 - should accept read-only arrays", () => {
565+
const ArrayType = types.array(types.string)
566+
const data = ["foo", "bar"] as const
567+
const instance = ArrayType.create(data)
568+
expect(getSnapshot(instance)).toEqual(["foo", "bar"])
569+
})

packages/mobx-state-tree/src/types/complex-types/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface IMSTArray<IT extends IAnyType> extends IObservableArray<IT["Typ
7272

7373
/** @hidden */
7474
export interface IArrayType<IT extends IAnyType>
75-
extends IType<IT["CreationType"][] | undefined, IT["SnapshotType"][], IMSTArray<IT>> {
75+
extends IType<readonly IT["CreationType"][] | undefined, IT["SnapshotType"][], IMSTArray<IT>> {
7676
hooks(hooks: IHooksGetter<IMSTArray<IAnyType>>): IArrayType<IT>
7777
}
7878

@@ -81,7 +81,7 @@ export interface IArrayType<IT extends IAnyType>
8181
* @hidden
8282
*/
8383
export class ArrayType<IT extends IAnyType> extends ComplexType<
84-
IT["CreationType"][] | undefined,
84+
readonly IT["CreationType"][] | undefined,
8585
IT["SnapshotType"][],
8686
IMSTArray<IT>
8787
> {

0 commit comments

Comments
 (0)