Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion __tests__/type-external.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isType, JSONArray, JSONObject, JSONTypes} from "type-plus"
import {Draft} from "../src/types/types-external"
import {createDraft, current, original} from "../src/immer"
import {createDraft, current, original, produce} from "../src/immer"

describe("Draft<T>", () => {
test("can use JSONTypes as T", () => {
Expand All @@ -17,6 +17,23 @@ describe("Draft<T>", () => {
type A = Draft<[string, number, JSONArray, JSONObject]>
isType.equal<true, [string, number, JSONArray, JSONObject], A>()
})

it("can use recursive types containing readonly arrays as T (#839)", () => {
// Same shape as type-fest's JsonValue
type JsonValue = string | number | boolean | null | JsonObject | JsonArray
type JsonObject = {[Key in string]: JsonValue} & {
[Key in string]?: JsonValue | undefined
}
type JsonArray = JsonValue[] | readonly JsonValue[]

type A = Draft<readonly JsonValue[]>
isType.equal<true, Draft<JsonValue>[], A>()

const base: {readonly items: readonly JsonValue[]} = {items: []}
produce(base, draft => {
draft.items.push({a: [1, "b"]})
})
})
})

describe("current() typings", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/types-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type IfAvailable<T, Fallback = void> =
*/
type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>

export type WritableDraft<T> = T extends any[]
export type WritableDraft<T> = T extends readonly any[]
? number extends T["length"]
? IsPlainArray<T> extends true
? Draft<T[number]>[]
Expand Down