draft.ts
This file
Path
__tests__/draft.ts
301 lines · 6.6 KiB · TypeScript
Last changed by
test(ts): improved exact type detectionaleclarsonbefore adoption2b7c21e
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
line 7why, one step back7// prettier-ignoretest(ts): improved exact type detectionaleclarsonbefore adoption2b7c21e
lines 8–10why, one step back8type Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0)9 ? (A extends B ? (B extends A ? unknown : never) : never)10 : neverchore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 11why, one step back11test(ts): improved exact type detectionaleclarsonbefore adoption2b7c21e
lines 12–16why, one step back12/** Fails when `actual` and `expected` have different types. */13declare const exactType: <Actual, Expected>(14 actual: Actual & Exact<Actual, Expected>,15 expected: Expected & Exact<Actual, Expected>16) => Expectedchore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 17–20why, one step back1718// To remove TS2454 errors.19declare const _: any20chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 21–30why, one step back21// DraftArray<T>22{23 // NOTE: As of 3.2.2, everything fails without "extends any"24 const $ = <Value extends any>(val: ReadonlyArray<Value>) => {25 val = _ as Draft<typeof val>26 val = _ as DraftArray<typeof val>27 let elem: Value = _ as Draft<Value>28 }29}30chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 31–33why, one step back31// Tuple32{33 let val: [1, 2] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 34–35why, one step back34 exactType(toDraft(val), val)35 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 36–40why, one step back36}3738// Tuple (nested in a tuple)39{40 let val: [[1, 2]] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 41–42why, one step back41 exactType(toDraft(val), val)42 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 43–44why, one step back43}44chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 45–52why, one step back45// Tuple (nested in two mutable arrays)46{47 let val: [1, 2][][] = _48 let draft: typeof val = _49 val = exactType(toDraft(val), draft)50 exactType(fromDraft(draft), val)51}52chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 53–56why, one step back53// Tuple (nested in two readonly arrays)54{55 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _56 let draft: [1, 2][][] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 57why, one step back57 val = exactType(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 58–59why, one step back58}59chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 60–69why, one step back60// Readonly tuple61{62 // TODO: Uncomment this when readonly tuples are supported.63 // More info: https://stackoverflow.com/a/53822074/222855964 // let val: Readonly<[1, 2]> = _65 // let draft: [1, 2] = _66 // draft = exactType(toDraft(val), draft)67 // val = fromDraft(draft)68}69chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 70–72why, one step back70// Mutable array71{72 let val: string[] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 73–74why, one step back73 exactType(toDraft(val), val)74 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 75–79why, one step back75}7677// Mutable array (nested in tuple)78{79 let val: [string[]] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 80–81why, one step back80 exactType(toDraft(val), val)81 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 82–87why, one step back82}8384// Readonly array85{86 let val: ReadonlyArray<string> = _87 let draft: string[] = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 88–89why, one step back88 val = exactType(toDraft(val), draft)89 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 90–95why, one step back90}9192// Readonly array (nested in readonly object)93{94 let val: {readonly a: ReadonlyArray<string>} = _95 let draft: {a: string[]} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 96–97why, one step back96 val = exactType(toDraft(val), draft)97 fromDraft(draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 98–102why, one step back98}99100// Mutable object101{102 let val: {a: 1} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 103–104why, one step back103 exactType(toDraft(val), val)104 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 105–109why, one step back105}106107// Mutable object (nested in mutable object)108{109 let val: {a: {b: 1}} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 110–111why, one step back110 exactType(toDraft(val), val)111 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 112–119why, one step back112}113114// Interface115{116 interface Foo {117 a: {b: number}118 }119 let val: Foo = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 120–121why, one step back120 exactType(toDraft(val), val)121 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 122–132why, one step back122}123124// Interface (nested in interface)125{126 interface Foo {127 a: {b: number}128 }129 interface Bar {130 foo: Foo131 }132 let val: Bar = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 133–134why, one step back133 exactType(toDraft(val), val)134 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 135–138why, one step back135}136137// Readonly object138{chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 139why, one step back139 let val: {readonly a: 1} = _chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 140why, one step back140 let draft: {a: 1} = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 141why, one step back141 val = exactType(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 142–145why, one step back142}143144// Readonly object (nested in tuple)145{chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 146–148why, one step back146 let val: [{readonly a: 1}] = _147 let draft: [{a: 1}] = _148 val = exactType(toDraft(val), draft)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 149–153why, one step back149}150151// Loose function152{153 let val: Function = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 154–155why, one step back154 exactType(toDraft(val), val)155 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 156–160why, one step back156}157158// Strict function159{160 let val: () => void = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 161–162why, one step back161 exactType(toDraft(val), val)162 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 163–164why, one step back163}164chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 165–190why, one step back165// Class type (mutable)166{167 class Foo {168 private test: any169 constructor(public bar: string) {}170 }171 let val: Foo = _172 // TODO: Uncomment this when plain object types can be distinguished from class types.173 // More info here: https://github.com/Microsoft/TypeScript/issues/29063174 // exactType(toDraft(val), val)175 // exactType(fromDraft(toDraft(val)), val)176}177178// Class type (readonly)179{180 class Foo {181 private test: any182 constructor(readonly bar: string) {}183 }184 let val: Foo = _185 // TODO: Uncomment this when plain object types can be distinguished from class types.186 // More info here: https://github.com/Microsoft/TypeScript/issues/29063187 // exactType(toDraft(val), val)188 // exactType(fromDraft(toDraft(val)), val)189}190chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 191–193why, one step back191// Map instance192{193 let val: Map<any, any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 194–195why, one step back194 exactType(toDraft(val), val)195 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 196–198why, one step back196197 // Weak maps198 let weak: WeakMap<any, any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 199–200why, one step back199 exactType(toDraft(weak), weak)200 exactType(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 201–205why, one step back201}202203// Set instance204{205 let val: Set<any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 206–207why, one step back206 exactType(toDraft(val), val)207 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 208–210why, one step back208209 // Weak sets210 let weak: WeakSet<any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 211–212why, one step back211 exactType(toDraft(weak), weak)212 exactType(fromDraft(toDraft(weak)), weak)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 213–217why, one step back213}214215// Promise object216{217 let val: Promise<any> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 218–219why, one step back218 exactType(toDraft(val), val)219 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 220–224why, one step back220}221222// Date instance223{224 let val: Date = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 225–226why, one step back225 exactType(toDraft(val), val)226 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 227–231why, one step back227}228229// RegExp instance230{231 let val: RegExp = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 232–233why, one step back232 exactType(toDraft(val), val)233 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 234–238why, one step back234}235236// Boxed primitive237{238 let val: Boolean = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 239–240why, one step back239 exactType(toDraft(val), val)240 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 241–245why, one step back241}242243// String literal244{245 let val: string = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 246–247why, one step back246 exactType(toDraft(val), val)247 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 248–252why, one step back248}249250// Any251{252 let val: any = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 253–254why, one step back253 exactType(toDraft(val), val)254 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 255–259why, one step back255}256257// Never258{259 let val: never = _ as neverchore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 260–261why, one step back260 exactType(toDraft(val), val)261 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 262–266why, one step back262}263264// Unknown265{266 let val: unknown = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 267–268why, one step back267 exactType(toDraft(val), val)268 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 269–273why, one step back269}270271// Numeral272{273 let val: 1 = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 274–275why, one step back274 exactType(toDraft(val), val)275 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 276–280why, one step back276}277278// Union of numerals279{280 let val: 1 | 2 | 3 = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 281–282why, one step back281 exactType(toDraft(val), val)282 exactType(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 283–287why, one step back283}284285// Union of tuple, array, object286{287 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
lines 288–300why, one step back288 let draft: [0] | string[] | {a: 1} = _289 val = exactType(toDraft(val), draft)290}291292// Generic type293{294 // NOTE: "extends any" only helps a little.295 const $ = <T extends any>(val: ReadonlyArray<T>) => {296 let draft: Draft<typeof val> = _297 val = exactType(toDraft(val), draft)298 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]299 // exactType(fromDraft(draft), draft)300 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 301why, one step back301}