draft.ts
This file
Path
__tests__/draft.ts
291 lines · 5.8 KiB · TypeScript
Last changed by
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
History of this fileTake it
test(ts): use spec.tsAlec Larsonbefore adoptionb5abaed
line 1why, one step back1import {assert, _} from "spec.ts"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 2why, one step back2import {Draft} from "../src/index"chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
lines 3–4why, one step back34// For checking if a type is assignable to its draft type (and vice versa)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 5–6why, one step back5const toDraft: <T>(value: T) => Draft<T> = x => x as any6const fromDraft: <T>(draft: Draft<T>) => T = x => x as anychore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 7why, one step back7Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 8–15why, one step back8test("draft.ts", () => {9 // DraftArray<T>10 {11 // NOTE: As of 3.2.2, everything fails without "extends any"12 ;<Value extends any>(val: ReadonlyArray<Value>) => {13 val = _ as Draft<typeof val>14 let elem: Value = _ as Draft<Value>15 }style: use tabsuse-tabsbefore adoptiona9c0fd0
line 16why, one step back16 }chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 17why, one step back17Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 18–23why, one step back18 // Tuple19 {20 let val: [1, 2] = _21 assert(toDraft(val), val)22 assert(fromDraft(toDraft(val)), val)23 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 24why, one step back24Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 25–30why, one step back25 // Tuple (nested in a tuple)26 {27 let val: [[1, 2]] = _28 assert(toDraft(val), val)29 assert(fromDraft(toDraft(val)), val)30 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 31why, one step back31Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 32–38why, one step back32 // Tuple (nested in two mutable arrays)33 {34 let val: [1, 2][][] = _35 let draft: typeof val = _36 val = assert(toDraft(val), draft)37 assert(fromDraft(draft), val)38 }chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 39why, one step back39Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 40–45why, one step back40 // Tuple (nested in two readonly arrays)41 {42 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _43 let draft: [1, 2][][] = _44 val = assert(toDraft(val), draft)45 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 46why, one step back46Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 47–55why, one step back47 // Readonly tuple48 {49 // TODO: Uncomment this when readonly tuples are supported.50 // More info: https://stackoverflow.com/a/53822074/222855951 // let val: Readonly<[1, 2]> = _52 // let draft: [1, 2] = _53 // draft = assert(toDraft(val), draft)54 // val = fromDraft(draft)55 }chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 56why, one step back56Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 57–62why, one step back57 // Mutable array58 {59 let val: string[] = _60 assert(toDraft(val), val)61 assert(fromDraft(toDraft(val)), val)62 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 63why, one step back63Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 64–69why, one step back64 // Mutable array (nested in tuple)65 {66 let val: [string[]] = _67 assert(toDraft(val), val)68 assert(fromDraft(toDraft(val)), val)69 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 70why, one step back70Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 71–77why, one step back71 // Readonly array72 {73 let val: ReadonlyArray<string> = _74 let draft: string[] = _75 val = assert(toDraft(val), draft)76 fromDraft(draft)77 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 78why, one step back78Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 79–85why, one step back79 // Readonly array (nested in readonly object)80 {81 let val: {readonly a: ReadonlyArray<string>} = _82 let draft: {a: string[]} = _83 val = assert(toDraft(val), draft)84 fromDraft(draft)85 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 86why, one step back86Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 87–92why, one step back87 // Mutable object88 {89 let val: {a: 1} = _90 assert(toDraft(val), val)91 assert(fromDraft(toDraft(val)), val)92 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 93why, one step back93Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 94–99why, one step back94 // Mutable object (nested in mutable object)95 {96 let val: {a: {b: 1}} = _97 assert(toDraft(val), val)98 assert(fromDraft(toDraft(val)), val)99 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 100why, one step back100Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 101–108why, one step back101 // Interface102 {103 interface Foo {104 a: {b: number}105 }106 let val: Foo = _107 assert(toDraft(val), val)108 assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 109why, one step back109 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 110why, one step back110Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 111–121why, one step back111 // Interface (nested in interface)112 {113 interface Foo {114 a: {b: number}115 }116 interface Bar {117 foo: Foo118 }119 let val: Bar = _120 assert(toDraft(val), val)121 assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 122why, one step back122 }Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 123–128why, one step back123124 // Readonly object125 {126 let val: {readonly a: 1} = _127 let draft: {a: 1} = _128 val = assert(toDraft(val), draft)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 129why, one step back129 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 130why, one step back130Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 131–136why, one step back131 // Readonly object (nested in tuple)132 {133 let val: [{readonly a: 1}] = _134 let draft: [{a: 1}] = _135 val = assert(toDraft(val), draft)136 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 137why, one step back137Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 138–143why, one step back138 // Loose function139 {140 let val: Function = _141 assert(toDraft(val), val)142 assert(fromDraft(toDraft(val)), val)143 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 144why, one step back144Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 145–150why, one step back145 // Strict function146 {147 let val: () => void = _148 assert(toDraft(val), val)149 assert(fromDraft(toDraft(val)), val)150 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 151why, one step back151Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 152–162why, one step back152 // Class type (mutable)153 {154 class Foo {155 private test: any156 constructor(public bar: string) {}157 }158 let val: Foo = _159 // TODO: Uncomment this when plain object types can be distinguished from class types.160 // More info here: https://github.com/Microsoft/TypeScript/issues/29063161 // assert(toDraft(val), val)162 // assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 163why, one step back163 }chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 164why, one step back164Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 165–175why, one step back165 // Class type (readonly)166 {167 class Foo {168 private test: any169 constructor(readonly 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 // assert(toDraft(val), val)175 // assert(fromDraft(toDraft(val)), val)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 176why, one step back176 }chore: improve TypeScript testsaleclarsonbefore adoptioncaa44dc
line 177why, one step back177Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 178–182why, one step back178 // Map instance179 {180 let val: Map<any, any> = _181 assert(toDraft(val), val)182 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 183why, one step back183Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 184–188why, one step back184 // Weak maps185 let weak: WeakMap<any, any> = _186 assert(toDraft(weak), weak)187 assert(fromDraft(toDraft(weak)), weak)188 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 189why, one step back189Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 190–194why, one step back190 // Set instance191 {192 let val: Set<any> = _193 assert(toDraft(val), val)194 assert(fromDraft(toDraft(val)), val)chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 195why, one step back195Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 196–200why, one step back196 // Weak sets197 let weak: WeakSet<any> = _198 assert(toDraft(weak), weak)199 assert(fromDraft(toDraft(weak)), weak)200 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 201why, one step back201Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 202–207why, one step back202 // Promise object203 {204 let val: Promise<any> = _205 assert(toDraft(val), val)206 assert(fromDraft(toDraft(val)), val)207 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 208why, one step back208Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 209–214why, one step back209 // Date instance210 {211 let val: Date = _212 assert(toDraft(val), val)213 assert(fromDraft(toDraft(val)), val)214 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 215why, one step back215Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 216–221why, one step back216 // RegExp instance217 {218 let val: RegExp = _219 assert(toDraft(val), val)220 assert(fromDraft(toDraft(val)), val)221 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 222why, one step back222Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 223–228why, one step back223 // Boxed primitive224 {225 let val: Boolean = _226 assert(toDraft(val), val)227 assert(fromDraft(toDraft(val)), val)228 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 229why, one step back229Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 230–235why, one step back230 // String literal231 {232 let val: string = _233 assert(toDraft(val), val)234 assert(fromDraft(toDraft(val)), val)235 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 236why, one step back236Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 237–242why, one step back237 // Any238 {239 let val: any = _240 assert(toDraft(val), val)241 assert(fromDraft(toDraft(val)), val)242 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 243why, one step back243Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 244–249why, one step back244 // Never245 {246 let val: never = _ as never247 assert(toDraft(val), val)248 assert(fromDraft(toDraft(val)), val)249 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 250why, one step back250Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 251–256why, one step back251 // Unknown252 {253 let val: unknown = _254 assert(toDraft(val), val)255 assert(fromDraft(toDraft(val)), val)256 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 257why, one step back257Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 258–263why, one step back258 // Numeral259 {260 let val: 1 = _261 assert(toDraft(val), val)262 assert(fromDraft(toDraft(val)), val)263 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 264why, one step back264Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 265–270why, one step back265 // Union of numerals266 {267 let val: 1 | 2 | 3 = _268 assert(toDraft(val), val)269 assert(fromDraft(toDraft(val)), val)270 }chore: put Draft<T> tests in their own filealeclarsonbefore adoption544122b
line 271why, one step back271Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 272–275why, one step back272 // Union of tuple, array, object273 {274 let val: [0] | ReadonlyArray<string> | Readonly<{a: 1}> = _275 let draft: [0] | string[] | {a: 1} = _style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 276–277why, one step back276 val = assert(toDraft(val), draft)277 }Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 278–291why, one step back278279 // Generic type280 {281 // NOTE: "extends any" only helps a little.282 const $ = <T extends any>(val: ReadonlyArray<T>) => {283 let draft: Draft<typeof val> = _284 val = assert(toDraft(val), draft)285 // $ExpectError: [ts] Argument of type 'DraftArray<T>' is not assignable to parameter of type 'Draft<T>'. [2345]286 // assert(fromDraft(draft), draft)287 }288 }289290 expect(true).toBe(true)291})