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 fileTake it
test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 1why, one step back1import {assert, _} from "spec.ts"feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
line 2why, one step back2import produce, {Immutable, castImmutable} from "../src/index"fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
line 3why, one step back3feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 4–9why, one step back4test("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 back10feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 11–15why, one step back11 // tuple in array12 {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 back16feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 17–21why, one step back17 // tuple in tuple18 {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 back22feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 23–27why, one step back23 // array in array24 {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 back28feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 29–33why, one step back29 // tuple in object30 {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 back34feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 35–39why, one step back35 // object in tuple36 {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 back40feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 41–45why, one step back41 // array in object42 {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 back46feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 47–51why, one step back47 // object in array48 {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 back52feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 53–58why, one step back53 // object in object54 {55 let val = _ as Immutable<{a: {b: string}}>56 assert(val, _ as {readonly a: {readonly b: string}})57 }58feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 59–94why, one step back59 // Map60 {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 }94feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 95why, one step back95 expect(true).toBe(true)feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 96–116why, one step back96})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 back117})