draft.ts
This file
Path
__tests__/draft.ts
300 lines · 6.6 KiB · TypeScript
Last changed by
chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
History of this fileTake it
chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 1why, one step back1import {Draft, DraftArray} from "../dist/immer.js"chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 2–6why, one step back23// For checking if a type is assignable to its draft type (and vice versa)4declare const toDraft: <T>(value: T) => Draft<T>5declare const fromDraft: <T>(draft: Draft<T>) => T6chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 7–9why, one step back7// prettier-ignore8type AssertEqual<T, U> = (<G>() => G extends T ? 1 : 0) extends (<G>() => G extends U ? 1 : 0) ? unknown : never9chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 10why, one step back10/** Trigger a compiler error when a value is _not_ an exact type. */chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 11–14why, one step back11declare const exactType: <T, U>(12 draft: T & AssertEqual<T, U>,13 expected: U & AssertEqual<T, U>14) => Uchore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 15–18why, one step back1516// To remove TS2454 errors.17declare const _: any18chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 19–28why, one step back19// DraftArray<T>20{21 // NOTE: As of 3.2.2, everything fails without "extends any"22 const $ = <Value extends any>(val: ReadonlyArray<Value>) => {23 val = _ as Draft<typeof val>24 val = _ as DraftArray<typeof val>25 let elem: Value = _ as Draft<Value>26 }27}28chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 29–31why, one step back29// Tuple30{31 let val: [1, 2] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 32–33why, one step back32 exactType(toDraft(val), val)33 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 34–38why, one step back34}3536// Tuple (nested in a tuple)37{38 let val: [[1, 2]] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 39–40why, one step back39 exactType(toDraft(val), val)40 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 41–42why, one step back41}42chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 43–50why, one step back43// Tuple (nested in two mutable arrays)44{45 let val: [1, 2][][] = _46 let draft: typeof val = _47 val = exactType(toDraft(val), draft)48 exactType(fromDraft(draft), val)49}50chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 51–54why, one step back51// Tuple (nested in two readonly arrays)52{53 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _54 let draft: [1, 2][][] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 55–56why, one step back55 val = exactType(toDraft(val), draft)56 exactType(fromDraft(draft), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 57–58why, one step back57}58chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 59–68why, one step back59// Readonly tuple60{61 // TODO: Uncomment this when readonly tuples are supported.62 // More info: https://stackoverflow.com/a/53822074/222855963 // let val: Readonly<[1, 2]> = _64 // let draft: [1, 2] = _65 // draft = exactType(toDraft(val), draft)66 // val = fromDraft(draft)67}68chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 69–71why, one step back69// Mutable array70{71 let val: string[] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 72–73why, one step back72 exactType(toDraft(val), val)73 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 74–78why, one step back74}7576// Mutable array (nested in tuple)77{78 let val: [string[]] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 79–80why, one step back79 exactType(toDraft(val), val)80 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 81–86why, one step back81}8283// Readonly array84{85 let val: ReadonlyArray<string> = _86 let draft: string[] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 87–88why, one step back87 val = exactType(toDraft(val), draft)88 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 89–94why, one step back89}9091// Readonly array (nested in readonly object)92{93 let val: {readonly a: ReadonlyArray<string>} = _94 let draft: {a: string[]} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 95–96why, one step back95 val = exactType(toDraft(val), draft)96 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 97–101why, one step back97}9899// Mutable object100{101 let val: {a: 1} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 102–103why, one step back102 exactType(toDraft(val), val)103 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 104–108why, one step back104}105106// Mutable object (nested in mutable object)107{108 let val: {a: {b: 1}} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 109–110why, one step back109 exactType(toDraft(val), val)110 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 111–118why, one step back111}112113// Interface114{115 interface Foo {116 a: {b: number}117 }118 let val: Foo = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 119–120why, one step back119 exactType(toDraft(val), val)120 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 121–131why, one step back121}122123// Interface (nested in interface)124{125 interface Foo {126 a: {b: number}127 }128 interface Bar {129 foo: Foo130 }131 let val: Bar = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 132–133why, one step back132 exactType(toDraft(val), val)133 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 134–137why, one step back134}135136// Readonly object137{chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 138why, one step back138 let val: {readonly a: 1} = _chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 139why, one step back139 let draft: {a: 1} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 140why, one step back140 val = exactType(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 141–144why, one step back141}142143// Readonly object (nested in tuple)144{chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 145–147why, one step back145 let val: [{readonly a: 1}] = _146 let draft: [{a: 1}] = _147 val = exactType(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 148–152why, one step back148}149150// Loose function151{152 let val: Function = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 153–154why, one step back153 exactType(toDraft(val), val)154 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 155–159why, one step back155}156157// Strict function158{159 let val: () => void = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 160–161why, one step back160 exactType(toDraft(val), val)161 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 162–163why, one step back162}163chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 164–189why, one step back164// Class type (mutable)165{166 class Foo {167 private test: any168 constructor(public 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 // exactType(toDraft(val), val)174 // exactType(fromDraft(toDraft(val)), val)175}176177// Class type (readonly)178{179 class Foo {180 private test: any181 constructor(readonly bar: string) {}182 }183 let val: Foo = _184 // TODO: Uncomment this when plain object types can be distinguished from class types.185 // More info here: https://github.com/Microsoft/TypeScript/issues/29063186 // exactType(toDraft(val), val)187 // exactType(fromDraft(toDraft(val)), val)188}189chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 190–192why, one step back190// Map instance191{192 let val: Map<any, any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 193–194why, one step back193 exactType(toDraft(val), val)194 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 195–197why, one step back195196 // Weak maps197 let weak: WeakMap<any, any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 198–199why, one step back198 exactType(toDraft(weak), weak)199 exactType(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 200–204why, one step back200}201202// Set instance203{204 let val: Set<any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 205–206why, one step back205 exactType(toDraft(val), val)206 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 207–209why, one step back207208 // Weak sets209 let weak: WeakSet<any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 210–211why, one step back210 exactType(toDraft(weak), weak)211 exactType(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 212–216why, one step back212}213214// Promise object215{216 let val: Promise<any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 217–218why, one step back217 exactType(toDraft(val), val)218 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 219–223why, one step back219}220221// Date instance222{223 let val: Date = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 224–225why, one step back224 exactType(toDraft(val), val)225 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 226–230why, one step back226}227228// RegExp instance229{230 let val: RegExp = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 231–232why, one step back231 exactType(toDraft(val), val)232 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 233–237why, one step back233}234235// Boxed primitive236{237 let val: Boolean = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 238–239why, one step back238 exactType(toDraft(val), val)239 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 240–244why, one step back240}241242// String literal243{244 let val: string = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 245–246why, one step back245 exactType(toDraft(val), val)246 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 247–251why, one step back247}248249// Any250{251 let val: any = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 252–253why, one step back252 exactType(toDraft(val), val)253 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 254–258why, one step back254}255256// Never257{258 let val: never = _ as neverchore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 259–260why, one step back259 exactType(toDraft(val), val)260 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 261–265why, one step back261}262263// Unknown264{265 let val: unknown = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 266–267why, one step back266 exactType(toDraft(val), val)267 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 268–272why, one step back268}269270// Numeral271{272 let val: 1 = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 273–274why, one step back273 exactType(toDraft(val), val)274 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 275–279why, one step back275}276277// Union of numerals278{279 let val: 1 | 2 | 3 = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 280–281why, one step back280 exactType(toDraft(val), val)281 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 282–286why, one step back282}283284// Union of tuple, array, object285{286 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 287–299why, one step back287 let draft: [0] | string[] | {a: 1} = _288 val = exactType(toDraft(val), draft)289}290291// Generic type292{293 // NOTE: "extends any" only helps a little.294 const $ = <T extends any>(val: ReadonlyArray<T>) => {295 let draft: Draft<typeof val> = _296 val = exactType(toDraft(val), draft)297 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]298 // exactType(fromDraft(draft), draft)299 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 300why, one step back300}