draft.ts
This file
Path
__tests__/draft.ts
335 lines · 6.6 KiB · TypeScript
Last changed by
Immer 7Michel Weststratebefore adoption467ea5d
History of this fileTake it
The walk stopped after 200 commits: lines older than f72f69b point at it.
test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 1why, one step back1import {assert, _} from "spec.ts"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 2–9why, one step back2import produce, {3 Draft,4 castDraft,5 original,6 enableAllPlugins7} from "../src/immer"89enableAllPlugins()docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
lines 10–11why, one step back1011// For checking if a type is assignable to its draft type (and vice versa)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 12–13why, one step back12const toDraft: <T>(value: T) => Draft<T> = x => x as any13const fromDraft: <T>(draft: Draft<T>) => T = x => x as anydocs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 14why, one step back14feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 15–21why, one step back15test("draft.ts", () => {16 // Tuple17 {18 let val: [1, 2] = _19 assert(toDraft(val), val)20 assert(fromDraft(toDraft(val)), val)21 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 22why, one step back22feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 23–28why, one step back23 // Tuple (nested in a tuple)24 {25 let val: [[1, 2]] = _26 assert(toDraft(val), val)27 assert(fromDraft(toDraft(val)), val)28 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 29why, one step back29feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 30–36why, one step back30 // Tuple (nested in two mutable arrays)31 {32 let val: [1, 2][][] = _33 let draft: typeof val = _34 val = assert(toDraft(val), draft)35 assert(fromDraft(draft), val)36 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 37why, one step back37feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 38–43why, one step back38 // Tuple (nested in two readonly arrays)39 {40 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _41 let draft: [1, 2][][] = _42 val = assert(toDraft(val), draft)43 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 44why, one step back44feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 45–53why, one step back45 // Readonly tuple46 {47 // TODO: Uncomment this when readonly tuples are supported.48 // More info: https://stackoverflow.com/a/53822074/222855949 // let val: Readonly<[1, 2]> = _50 // let draft: [1, 2] = _51 // draft = assert(toDraft(val), draft)52 // val = fromDraft(draft)53 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 54why, one step back54feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 55–60why, one step back55 // Mutable array56 {57 let val: string[] = _58 assert(toDraft(val), val)59 assert(fromDraft(toDraft(val)), val)60 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 61why, one step back61feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 62–67why, one step back62 // Mutable array (nested in tuple)63 {64 let val: [string[]] = _65 assert(toDraft(val), val)66 assert(fromDraft(toDraft(val)), val)67 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 68why, one step back68feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 69–75why, one step back69 // Readonly array70 {71 let val: ReadonlyArray<string> = _72 let draft: string[] = _73 val = assert(toDraft(val), draft)74 fromDraft(draft)75 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 76why, one step back76feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 77–83why, one step back77 // Readonly array (nested in readonly object)78 {79 let val: {readonly a: ReadonlyArray<string>} = _80 let draft: {a: string[]} = _81 val = assert(toDraft(val), draft)82 fromDraft(draft)83 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 84why, one step back84feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 85–90why, one step back85 // Mutable object86 {87 let val: {a: 1} = _88 assert(toDraft(val), val)89 assert(fromDraft(toDraft(val)), val)90 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 91why, one step back91feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 92–97why, one step back92 // Mutable object (nested in mutable object)93 {94 let val: {a: {b: 1}} = _95 assert(toDraft(val), val)96 assert(fromDraft(toDraft(val)), val)97 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 98why, one step back98feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 99–106why, one step back99 // Interface100 {101 interface Foo {102 a: {b: number}103 }104 let val: Foo = _105 assert(toDraft(val), val)106 assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 107why, one step back107 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 108why, one step back108feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 109–119why, one step back109 // Interface (nested in interface)110 {111 interface Foo {112 a: {b: number}113 }114 interface Bar {115 foo: Foo116 }117 let val: Bar = _118 assert(toDraft(val), val)119 assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 120why, one step back120 }feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 121–126why, one step back121122 // Readonly object123 {124 let val: {readonly a: 1} = _125 let draft: {a: 1} = _126 val = assert(toDraft(val), draft)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 127why, one step back127 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 128why, one step back128feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 129–134why, one step back129 // Readonly object (nested in tuple)130 {131 let val: [{readonly a: 1}] = _132 let draft: [{a: 1}] = _133 val = assert(toDraft(val), draft)134 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 135why, one step back135feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 136–141why, one step back136 // Loose function137 {138 let val: Function = _139 assert(toDraft(val), val)140 assert(fromDraft(toDraft(val)), val)141 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 142why, one step back142feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 143–148why, one step back143 // Strict function144 {145 let val: () => void = _146 assert(toDraft(val), val)147 assert(fromDraft(toDraft(val)), val)148 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 149why, one step back149feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 150–160why, one step back150 // Class type (mutable)151 {152 class Foo {153 private test: any154 constructor(public bar: string) {}155 }156 let val: Foo = _157 // TODO: Uncomment this when plain object types can be distinguished from class types.158 // More info here: https://github.com/Microsoft/TypeScript/issues/29063159 // assert(toDraft(val), val)160 // assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 161why, one step back161 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 162why, one step back162feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 163–173why, one step back163 // Class type (readonly)164 {165 class Foo {166 private test: any167 constructor(readonly bar: string) {}168 }169 let val: Foo = _170 // TODO: Uncomment this when plain object types can be distinguished from class types.171 // More info here: https://github.com/Microsoft/TypeScript/issues/29063172 // assert(toDraft(val), val)173 // assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 174why, one step back174 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 175why, one step back175feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 176–180why, one step back176 // Map instance177 {178 let val: Map<any, any> = _179 assert(toDraft(val), val)180 assert(fromDraft(toDraft(val)), val)docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 181why, one step back181feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 182–186why, one step back182 // Weak maps183 let weak: WeakMap<any, any> = _184 assert(toDraft(weak), weak)185 assert(fromDraft(toDraft(weak)), weak)186 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 187why, one step back187feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 188–194why, one step back188 // ReadonlyMap instance189 {190 let val: ReadonlyMap<any, any> = _191 let draft: Map<any, any> = _192 assert(toDraft(val), draft)193 }194feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 195–199why, one step back195 // Set instance196 {197 let val: Set<any> = _198 assert(toDraft(val), val)199 assert(fromDraft(toDraft(val)), val)docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 200why, one step back200feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 201–205why, one step back201 // Weak sets202 let weak: WeakSet<any> = _203 assert(toDraft(weak), weak)204 assert(fromDraft(toDraft(weak)), weak)205 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 206why, one step back206feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 207–213why, one step back207 // ReadonlySet instance208 {209 let val: ReadonlySet<any> = _210 let draft: Set<any> = _211 assert(toDraft(val), draft)212 }213feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 214–219why, one step back214 // Promise object215 {216 let val: Promise<any> = _217 assert(toDraft(val), val)218 assert(fromDraft(toDraft(val)), val)219 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 220why, one step back220feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 221–226why, one step back221 // Date instance222 {223 let val: Date = _224 assert(toDraft(val), val)225 assert(fromDraft(toDraft(val)), val)226 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 227why, one step back227feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 228–233why, one step back228 // RegExp instance229 {230 let val: RegExp = _231 assert(toDraft(val), val)232 assert(fromDraft(toDraft(val)), val)233 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 234why, one step back234feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 235–240why, one step back235 // Boxed primitive236 {237 let val: Boolean = _238 assert(toDraft(val), val)239 assert(fromDraft(toDraft(val)), val)240 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 241why, one step back241feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 242–247why, one step back242 // String literal243 {244 let val: string = _245 assert(toDraft(val), val)246 assert(fromDraft(toDraft(val)), val)247 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 248why, one step back248feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 249–254why, one step back249 // Any250 {251 let val: any = _252 assert(toDraft(val), val)253 assert(fromDraft(toDraft(val)), val)254 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 255why, one step back255feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 256–261why, one step back256 // Never257 {258 let val: never = _ as never259 assert(toDraft(val), val)260 assert(fromDraft(toDraft(val)), val)261 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 262why, one step back262feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 263–268why, one step back263 // Unknown264 {265 let val: unknown = _266 assert(toDraft(val), val)267 assert(fromDraft(toDraft(val)), val)268 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 269why, one step back269feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 270–275why, one step back270 // Numeral271 {272 let val: 1 = _273 assert(toDraft(val), val)274 assert(fromDraft(toDraft(val)), val)275 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 276why, one step back276feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 277–282why, one step back277 // Union of numerals278 {279 let val: 1 | 2 | 3 = _280 assert(toDraft(val), val)281 assert(fromDraft(toDraft(val)), val)282 }docs: mention the "immerable" symbolaleclarsonbefore adoptionf72f69b
line 283why, one step back283feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 284–287why, one step back284 // Union of tuple, array, object285 {286 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _287 let draft: [0] | string[] | {a: 1} = _style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 288–289why, one step back288 val = assert(toDraft(val), draft)289 }feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 290–295why, one step back290291 // Generic type292 {293 // NOTE: "extends any" only helps a little.294 const $ = <T extends any>(val: ReadonlyArray<T>) => {295 let draft: Draft<typeof val> = _Immer 7Michel Weststratebefore adoption467ea5d
line 296why, one step back296 assert(toDraft(val), draft)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 297–302why, one step back297 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]298 // assert(fromDraft(draft), draft)299 }300 }301302 expect(true).toBe(true)feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 303–304why, one step back303})304Merge pull request #531 from denis-sokolov/as-draft-namingMichel Weststratebefore adoptionc7e9de8
line 305why, one step back305test("castDraft", () => {feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 306–326why, one step back306 type Todo = {readonly done: boolean}307308 type State = {309 readonly finishedTodos: ReadonlyArray<Todo>310 readonly unfinishedTodos: ReadonlyArray<Todo>311 }312313 function markAllFinished(state: State) {314 produce(state, draft => {315 draft.finishedTodos = castDraft(state.unfinishedTodos)316 })317 }318})319320test("#505 original", () => {321 const baseState = {users: [{name: "Richie"}] as const}322 const nextState = produce(baseState, draftState => {323 original(draftState.users) === baseState.users324 })325})326Merge pull request #531 from denis-sokolov/as-draft-namingMichel Weststratebefore adoptionc7e9de8
line 327why, one step back327test("castDraft preserves a value", () => {feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 328–329why, one step back328 const x = {}329 expect(castDraft(x)).toBe(x)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 330why, one step back330})fix: createDraft should return `Draft` instead of `Drafted`. Fixes #512Michel Weststratebefore adoption3f15d49
lines 331–335why, one step back331332test("#512 createDraft creates a draft", () => {333 const x = {y: 1}334 assert(x, _ as Draft<{y: number}>)335})