Skip to content
Myr

immutable.ts

This file

Path

__tests__/immutable.ts

117 lines · 2.4 KiB · TypeScript

Last changed by

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

History of this file

Take it

test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed

line 1why, one step back
1import {assert, _} from "spec.ts"

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

line 2why, one step back
2import produce, {Immutable, castImmutable} from "../src/index"

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 3why, one step back
3

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 4–9why, one step back
4test("types are ok", () => {
5 // array in tuple6 {7 let val = _ as Immutable<[string[], 1]>8 assert(val, _ as readonly [ReadonlyArray<string>, 1])9 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 10why, one step back
10

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 11–15why, one step back
11 // tuple in array
12 {13 let val = _ as Immutable<[string, 1][]>14 assert(val, _ as ReadonlyArray<readonly [string, 1]>)15 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 16why, one step back
16

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 17–21why, one step back
17 // tuple in tuple
18 {19 let val = _ as Immutable<[[string, 1], 1]>20 assert(val, _ as readonly [readonly [string, 1], 1])21 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 22why, one step back
22

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 23–27why, one step back
23 // array in array
24 {25 let val = _ as Immutable<string[][]>26 assert(val, _ as ReadonlyArray<ReadonlyArray<string>>)27 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 28why, one step back
28

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 29–33why, one step back
29 // tuple in object
30 {31 let val = _ as Immutable<{a: [string, 1]}>32 assert(val, _ as {readonly a: readonly [string, 1]})33 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 34why, one step back
34

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 35–39why, one step back
35 // object in tuple
36 {37 let val = _ as Immutable<[{a: string}, 1]>38 assert(val, _ as readonly [{readonly a: string}, 1])39 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 40why, one step back
40

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 41–45why, one step back
41 // array in object
42 {43 let val = _ as Immutable<{a: string[]}>44 assert(val, _ as {readonly a: ReadonlyArray<string>})45 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 46why, one step back
46

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 47–51why, one step back
47 // object in array
48 {49 let val = _ as Immutable<Array<{a: string}>>50 assert(val, _ as ReadonlyArray<{readonly a: string}>)51 }

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 52why, one step back
52

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 53–58why, one step back
53 // object in object
54 {55 let val = _ as Immutable<{a: {b: string}}>56 assert(val, _ as {readonly a: {readonly b: string}})57 }58

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 59–94why, one step back
59 // Map
60 {61 let val = _ as Immutable<Map<string, string>>62 assert(val, _ as ReadonlyMap<string, string>)63 }6465 // Already immutable Map66 {67 let val = _ as Immutable<ReadonlyMap<string, string>>68 assert(val, _ as ReadonlyMap<string, string>)69 }7071 // object in Map72 {73 let val = _ as Immutable<Map<{a: string}, {b: string}>>74 assert(val, _ as ReadonlyMap<{readonly a: string}, {readonly b: string}>)75 }7677 // Set78 {79 let val = _ as Immutable<Set<string>>80 assert(val, _ as ReadonlySet<string>)81 }8283 // Already immutable Set84 {85 let val = _ as Immutable<ReadonlySet<string>>86 assert(val, _ as ReadonlySet<string>)87 }8889 // object in Set90 {91 let val = _ as Immutable<Set<{a: string}>>92 assert(val, _ as ReadonlySet<{readonly a: string}>)93 }94

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 95why, one step back
95 expect(true).toBe(true)

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 96–116why, one step back
96})
9798test("#381 produce immutable state", () => {99 const someState = {100 todos: [101 {102 done: false103 }104 ]105 }106107 const immutable = castImmutable(produce(someState, _draft => {}))108 assert(109 immutable,110 _ as {readonly todos: ReadonlyArray<{readonly done: boolean}>}111 )112})113114test("castImmutable preserves a value", () => {115 const x = {}116 expect(castImmutable(x)).toBe(x)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 117why, one step back
117})