draft.ts
This file
Path
__tests__/draft.ts
287 lines · 6.0 KiB · TypeScript
Last changed by
test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
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–10why, one step back8// DraftArray<T>9{10 // NOTE: As of 3.2.2, everything fails without "extends any"test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 11why, one step back11 ;<Value extends any>(val: ReadonlyArray<Value>) => {chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 12–16why, one step back12 val = _ as Draft<typeof val>13 let elem: Value = _ as Draft<Value>14 }15}16chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 17–19why, one step back17// Tuple18{19 let val: [1, 2] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 20–21why, one step back20 assert(toDraft(val), val)21 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 22–26why, one step back22}2324// Tuple (nested in a tuple)25{26 let val: [[1, 2]] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 27–28why, one step back27 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–34why, one step back31// Tuple (nested in two mutable arrays)32{33 let val: [1, 2][][] = _34 let draft: typeof val = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 35–36why, one step back35 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–42why, one step back39// Tuple (nested in two readonly arrays)40{41 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _42 let draft: [1, 2][][] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 43why, one step back43 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–51why, one step back46// Readonly tuple47{48 // 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] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 52why, one step back52 // draft = assert(toDraft(val), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 53–55why, one step back53 // val = fromDraft(draft)54}55chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 56–58why, one step back56// Mutable array57{58 let val: string[] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 59–60why, one step back59 assert(toDraft(val), val)60 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 61–65why, one step back61}6263// Mutable array (nested in tuple)64{65 let val: [string[]] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 66–67why, one step back66 assert(toDraft(val), val)67 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 68–73why, one step back68}6970// Readonly array71{72 let val: ReadonlyArray<string> = _73 let draft: string[] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 74why, one step back74 val = assert(toDraft(val), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 75why, one step back75 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 76–81why, one step back76}7778// Readonly array (nested in readonly object)79{80 let val: {readonly a: ReadonlyArray<string>} = _81 let draft: {a: string[]} = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 82why, one step back82 val = assert(toDraft(val), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 83why, one step back83 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 84–88why, one step back84}8586// Mutable object87{88 let val: {a: 1} = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 89–90why, one step back89 assert(toDraft(val), val)90 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 91–95why, one step back91}9293// Mutable object (nested in mutable object)94{95 let val: {a: {b: 1}} = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 96–97why, one step back96 assert(toDraft(val), val)97 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 98–105why, one step back98}99100// Interface101{102 interface Foo {103 a: {b: number}104 }105 let val: Foo = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 106–107why, one step back106 assert(toDraft(val), val)107 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 108–118why, one step back108}109110// Interface (nested in interface)111{112 interface Foo {113 a: {b: number}114 }115 interface Bar {116 foo: Foo117 }118 let val: Bar = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 119–120why, one step back119 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{chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 125why, one step back125 let val: {readonly a: 1} = _chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 126why, one step back126 let draft: {a: 1} = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 127why, one step back127 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{chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 132–133why, one step back132 let val: [{readonly a: 1}] = _133 let draft: [{a: 1}] = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 134why, one step back134 val = assert(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 135–139why, one step back135}136137// Loose function138{139 let val: Function = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 140–141why, one step back140 assert(toDraft(val), val)141 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 142–146why, one step back142}143144// Strict function145{146 let val: () => void = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 147–148why, one step back147 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–159why, one step back151// Class type (mutable)152{153 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/29063test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 160–161why, one step back160 // assert(toDraft(val), val)161 // assert(fromDraft(toDraft(val)), val)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 162–172why, one step back162}163164// Class type (readonly)165{166 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/29063test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 173–174why, one step back173 // 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–179why, one step back177// Map instance178{179 let val: Map<any, any> = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 180–181why, one step back180 assert(toDraft(val), val)181 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 182–184why, one step back182183 // Weak maps184 let weak: WeakMap<any, any> = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 185–186why, one step back185 assert(toDraft(weak), weak)186 assert(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 187–191why, one step back187}188189// Set instance190{191 let val: Set<any> = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 192–193why, one step back192 assert(toDraft(val), val)193 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 194–196why, one step back194195 // Weak sets196 let weak: WeakSet<any> = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 197–198why, one step back197 assert(toDraft(weak), weak)198 assert(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 199–203why, one step back199}200201// Promise object202{203 let val: Promise<any> = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 204–205why, one step back204 assert(toDraft(val), val)205 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 206–210why, one step back206}207208// Date instance209{210 let val: Date = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 211–212why, one step back211 assert(toDraft(val), val)212 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 213–217why, one step back213}214215// RegExp instance216{217 let val: RegExp = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 218–219why, one step back218 assert(toDraft(val), val)219 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 220–224why, one step back220}221222// Boxed primitive223{224 let val: Boolean = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 225–226why, one step back225 assert(toDraft(val), val)226 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 227–231why, one step back227}228229// String literal230{231 let val: string = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 232–233why, one step back232 assert(toDraft(val), val)233 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 234–238why, one step back234}235236// Any237{238 let val: any = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 239–240why, one step back239 assert(toDraft(val), val)240 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 241–245why, one step back241}242243// Never244{245 let val: never = _ as nevertest(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 246–247why, one step back246 assert(toDraft(val), val)247 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 248–252why, one step back248}249250// Unknown251{252 let val: unknown = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 253–254why, one step back253 assert(toDraft(val), val)254 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 255–259why, one step back255}256257// Numeral258{259 let val: 1 = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 260–261why, one step back260 assert(toDraft(val), val)261 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 262–266why, one step back262}263264// Union of numerals265{266 let val: 1 | 2 | 3 = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
lines 267–268why, one step back267 assert(toDraft(val), val)268 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 269–273why, one step back269}270271// Union of tuple, array, object272{273 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 274why, one step back274 let draft: [0] | string[] | {a: 1} = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 275why, one step back275 val = assert(toDraft(val), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 276–282why, one step back276}277278// Generic type279{280 // NOTE: "extends any" only helps a little.281 const $ = <T extends any>(val: ReadonlyArray<T>) => {282 let draft: Draft<typeof val> = _test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 283why, one step back283 val = assert(toDraft(val), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 284why, one step back284 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 285why, one step back285 // assert(fromDraft(draft), draft)chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 286why, one step back286 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 287why, one step back287}