draft.ts
This file
Path
__tests__/draft.ts
287 lines · 5.5 KiB · TypeScript
Last changed by
style: use tabsuse-tabsbefore adoptiona9c0fd0
History of this fileTake it
test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 1why, one step back1import {assert, _} from "spec.ts"refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
line 2why, one step back2import {Draft} from "../dist/immer.js"chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 3–7why, one step back34// For checking if a type is assignable to its draft type (and vice versa)5declare const toDraft: <T>(value: T) => Draft<T>6declare const fromDraft: <T>(draft: Draft<T>) => T7chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 8–9why, one step back8// DraftArray<T>9{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 10–14why, one step back10 // NOTE: As of 3.2.2, everything fails without "extends any"11 ;<Value extends any>(val: ReadonlyArray<Value>) => {12 val = _ as Draft<typeof val>13 let elem: Value = _ as Draft<Value>14 }chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 15–16why, one step back15}16chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 17–18why, one step back17// Tuple18{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 19–21why, one step back19 let val: [1, 2] = _20 assert(toDraft(val), val)21 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 22–25why, one step back22}2324// Tuple (nested in a tuple)25{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 26–28why, one step back26 let val: [[1, 2]] = _27 assert(toDraft(val), val)28 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 29–30why, one step back29}30chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 31–32why, one step back31// Tuple (nested in two mutable arrays)32{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 33–36why, one step back33 let val: [1, 2][][] = _34 let draft: typeof val = _35 val = assert(toDraft(val), draft)36 assert(fromDraft(draft), val)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 37–38why, one step back37}38chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 39–40why, one step back39// Tuple (nested in two readonly arrays)40{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 41–43why, one step back41 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _42 let draft: [1, 2][][] = _43 val = assert(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 44–45why, one step back44}45chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 46–47why, one step back46// Readonly tuple47{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 48–53why, one step back48 // TODO: Uncomment this when readonly tuples are supported.49 // More info: https://stackoverflow.com/a/53822074/222855950 // let val: Readonly<[1, 2]> = _51 // let draft: [1, 2] = _52 // draft = assert(toDraft(val), draft)53 // val = fromDraft(draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 54–55why, one step back54}55chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 56–57why, one step back56// Mutable array57{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 58–60why, one step back58 let val: string[] = _59 assert(toDraft(val), val)60 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 61–64why, one step back61}6263// Mutable array (nested in tuple)64{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 65–67why, one step back65 let val: [string[]] = _66 assert(toDraft(val), val)67 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 68–71why, one step back68}6970// Readonly array71{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 72–75why, one step back72 let val: ReadonlyArray<string> = _73 let draft: string[] = _74 val = assert(toDraft(val), draft)75 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 76–79why, one step back76}7778// Readonly array (nested in readonly object)79{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 80–83why, one step back80 let val: {readonly a: ReadonlyArray<string>} = _81 let draft: {a: string[]} = _82 val = assert(toDraft(val), draft)83 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 84–87why, one step back84}8586// Mutable object87{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 88–90why, one step back88 let val: {a: 1} = _89 assert(toDraft(val), val)90 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 91–94why, one step back91}9293// Mutable object (nested in mutable object)94{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 95–97why, one step back95 let val: {a: {b: 1}} = _96 assert(toDraft(val), val)97 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 98–101why, one step back98}99100// Interface101{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 102–107why, one step back102 interface Foo {103 a: {b: number}104 }105 let val: Foo = _106 assert(toDraft(val), val)107 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 108–111why, one step back108}109110// Interface (nested in interface)111{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 112–120why, one step back112 interface Foo {113 a: {b: number}114 }115 interface Bar {116 foo: Foo117 }118 let val: Bar = _119 assert(toDraft(val), val)120 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 121–124why, one step back121}122123// Readonly object124{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 125–127why, one step back125 let val: {readonly a: 1} = _126 let draft: {a: 1} = _127 val = assert(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 128–131why, one step back128}129130// Readonly object (nested in tuple)131{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 132–134why, one step back132 let val: [{readonly a: 1}] = _133 let draft: [{a: 1}] = _134 val = assert(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 135–138why, one step back135}136137// Loose function138{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 139–141why, one step back139 let val: Function = _140 assert(toDraft(val), val)141 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 142–145why, one step back142}143144// Strict function145{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 146–148why, one step back146 let val: () => void = _147 assert(toDraft(val), val)148 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 149–150why, one step back149}150chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 151–152why, one step back151// Class type (mutable)152{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 153–161why, one step back153 class Foo {154 private test: any155 constructor(public bar: string) {}156 }157 let val: Foo = _158 // TODO: Uncomment this when plain object types can be distinguished from class types.159 // More info here: https://github.com/Microsoft/TypeScript/issues/29063160 // assert(toDraft(val), val)161 // assert(fromDraft(toDraft(val)), val)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 162–165why, one step back162}163164// Class type (readonly)165{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 166–174why, one step back166 class Foo {167 private test: any168 constructor(readonly bar: string) {}169 }170 let val: Foo = _171 // TODO: Uncomment this when plain object types can be distinguished from class types.172 // More info here: https://github.com/Microsoft/TypeScript/issues/29063173 // assert(toDraft(val), val)174 // assert(fromDraft(toDraft(val)), val)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 175–176why, one step back175}176chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 177–178why, one step back177// Map instance178{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 179–181why, one step back179 let val: Map<any, any> = _180 assert(toDraft(val), val)181 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 182why, one step back182style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 183–186why, one step back183 // Weak maps184 let weak: WeakMap<any, any> = _185 assert(toDraft(weak), weak)186 assert(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 187–190why, one step back187}188189// Set instance190{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 191–193why, one step back191 let val: Set<any> = _192 assert(toDraft(val), val)193 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 194why, one step back194style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 195–198why, one step back195 // Weak sets196 let weak: WeakSet<any> = _197 assert(toDraft(weak), weak)198 assert(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 199–202why, one step back199}200201// Promise object202{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 203–205why, one step back203 let val: Promise<any> = _204 assert(toDraft(val), val)205 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 206–209why, one step back206}207208// Date instance209{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 210–212why, one step back210 let val: Date = _211 assert(toDraft(val), val)212 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 213–216why, one step back213}214215// RegExp instance216{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 217–219why, one step back217 let val: RegExp = _218 assert(toDraft(val), val)219 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 220–223why, one step back220}221222// Boxed primitive223{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 224–226why, one step back224 let val: Boolean = _225 assert(toDraft(val), val)226 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 227–230why, one step back227}228229// String literal230{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 231–233why, one step back231 let val: string = _232 assert(toDraft(val), val)233 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 234–237why, one step back234}235236// Any237{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 238–240why, one step back238 let val: any = _239 assert(toDraft(val), val)240 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 241–244why, one step back241}242243// Never244{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 245–247why, one step back245 let val: never = _ as never246 assert(toDraft(val), val)247 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 248–251why, one step back248}249250// Unknown251{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 252–254why, one step back252 let val: unknown = _253 assert(toDraft(val), val)254 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 255–258why, one step back255}256257// Numeral258{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 259–261why, one step back259 let val: 1 = _260 assert(toDraft(val), val)261 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 262–265why, one step back262}263264// Union of numerals265{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 266–268why, one step back266 let val: 1 | 2 | 3 = _267 assert(toDraft(val), val)268 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 269–272why, one step back269}270271// Union of tuple, array, object272{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 273–275why, one step back273 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _274 let draft: [0] | string[] | {a: 1} = _275 val = assert(toDraft(val), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 276–279why, one step back276}277278// Generic type279{style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 280–286why, one step back280 // NOTE: "extends any" only helps a little.281 const $ = <T extends any>(val: ReadonlyArray<T>) => {282 let draft: Draft<typeof val> = _283 val = assert(toDraft(val), draft)284 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]285 // assert(fromDraft(draft), draft)286 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 287why, one step back287}