|
| 1 | +/** |
| 2 | + * Type-level tests for Readable<T> / Writable<T> built-in object passthrough. |
| 3 | + * |
| 4 | + * These are pure compile-time assertions checked by `tsc --noEmit`. If any |
| 5 | + * assertion is wrong, the file fails to type-check. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { $Read, $Write, Readable, Writable } from "../src/index.js"; |
| 9 | + |
| 10 | +// Bidirectional structural assignability. Looser than the parametric `(<T>() => …)` |
| 11 | +// trick but enough for "the resulting shape preserves X" — mapped-type-produced |
| 12 | +// objects are structurally identical to their literal twins but fail strict |
| 13 | +// parametric equality. |
| 14 | +type Equals<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false; |
| 15 | +type Expect<T extends true> = T; |
| 16 | + |
| 17 | +// --- Date passthrough --- |
| 18 | + |
| 19 | +type _ReadableDate = Expect<Equals<Readable<Date>, Date>>; |
| 20 | +type _WritableDate = Expect<Equals<Writable<Date>, Date>>; |
| 21 | + |
| 22 | +// $Read<Date> unwraps to Date, not to a structurally-mapped Date prototype |
| 23 | +type _ReadableReadDate = Expect<Equals<Readable<$Read<Date>>, Date>>; |
| 24 | +type _WritableWriteDate = Expect<Equals<Writable<$Write<Date>>, Date>>; |
| 25 | + |
| 26 | +// Date inside an object field stays Date |
| 27 | +type _ReadableObjectWithDate = Expect<Equals<Readable<{ created: Date }>, { created: Date }>>; |
| 28 | +type _WritableObjectWithDate = Expect<Equals<Writable<{ created: Date }>, { created: Date }>>; |
| 29 | + |
| 30 | +// --- RegExp passthrough --- |
| 31 | + |
| 32 | +type _ReadableRegExp = Expect<Equals<Readable<RegExp>, RegExp>>; |
| 33 | +type _WritableRegExp = Expect<Equals<Writable<RegExp>, RegExp>>; |
| 34 | + |
| 35 | +type _ReadableObjectWithRegExp = Expect<Equals<Readable<{ pattern: RegExp }>, { pattern: RegExp }>>; |
| 36 | + |
| 37 | +// --- Function passthrough --- |
| 38 | + |
| 39 | +type Fn = (x: number) => string; |
| 40 | + |
| 41 | +type _ReadableFn = Expect<Equals<Readable<Fn>, Fn>>; |
| 42 | +type _WritableFn = Expect<Equals<Writable<Fn>, Fn>>; |
| 43 | + |
| 44 | +type _ReadableObjectWithFn = Expect<Equals<Readable<{ handler: Fn }>, { handler: Fn }>>; |
| 45 | + |
| 46 | +// --- Negative control: plain object still gets recursive treatment --- |
| 47 | + |
| 48 | +// Plain nested object's $Write marker is still stripped from Readable |
| 49 | +type _ReadableStripsWrite = Expect< |
| 50 | + Equals< |
| 51 | + Readable<{ id: number; password: $Write<string> }>, |
| 52 | + { id: number } |
| 53 | + > |
| 54 | +>; |
| 55 | + |
| 56 | +// Plain nested object's $Read marker is still stripped from Writable |
| 57 | +type _WritableStripsRead = Expect< |
| 58 | + Equals< |
| 59 | + Writable<{ id: $Read<number>; name: string }>, |
| 60 | + { name: string } & { id?: never } |
| 61 | + > |
| 62 | +>; |
0 commit comments