|
| 1 | +=== tests/cases/compiler/stringLiteralFreshnessInGenericTypeGuard.ts === |
| 2 | +interface Guard<T> { |
| 3 | +>Guard : Symbol(Guard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 0, 0)) |
| 4 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 0, 16)) |
| 5 | + |
| 6 | + (val: unknown): val is T; |
| 7 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 1, 3)) |
| 8 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 1, 3)) |
| 9 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 0, 16)) |
| 10 | +} |
| 11 | + |
| 12 | +type ObjectGuard<T> = { |
| 13 | +>ObjectGuard : Symbol(ObjectGuard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 2, 1)) |
| 14 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 4, 17)) |
| 15 | + |
| 16 | + [key in keyof T]: Guard<T[key]>; |
| 17 | +>key : Symbol(key, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 5, 3)) |
| 18 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 4, 17)) |
| 19 | +>Guard : Symbol(Guard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 0, 0)) |
| 20 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 4, 17)) |
| 21 | +>key : Symbol(key, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 5, 3)) |
| 22 | + |
| 23 | +}; |
| 24 | + |
| 25 | +function isObject(val: unknown): val is Record<string, unknown> { |
| 26 | +>isObject : Symbol(isObject, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 6, 2)) |
| 27 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 8, 18)) |
| 28 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 8, 18)) |
| 29 | +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) |
| 30 | + |
| 31 | + return val !== undefined && val !== null && typeof val === 'object' && !Array.isArray(val); |
| 32 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 8, 18)) |
| 33 | +>undefined : Symbol(undefined) |
| 34 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 8, 18)) |
| 35 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 8, 18)) |
| 36 | +>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --)) |
| 37 | +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) |
| 38 | +>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --)) |
| 39 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 8, 18)) |
| 40 | +} |
| 41 | + |
| 42 | +declare function createObjectGuard<T>(guard: ObjectGuard<T>): (val: unknown) => val is T; |
| 43 | +>createObjectGuard : Symbol(createObjectGuard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 10, 1)) |
| 44 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 35)) |
| 45 | +>guard : Symbol(guard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 38)) |
| 46 | +>ObjectGuard : Symbol(ObjectGuard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 2, 1)) |
| 47 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 35)) |
| 48 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 63)) |
| 49 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 63)) |
| 50 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 35)) |
| 51 | + |
| 52 | +declare function asLiteral<T extends (string | boolean | number)[]>(...literals: T): (val: unknown) => val is T[number] |
| 53 | +>asLiteral : Symbol(asLiteral, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 89)) |
| 54 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 14, 27)) |
| 55 | +>literals : Symbol(literals, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 14, 68)) |
| 56 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 14, 27)) |
| 57 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 14, 86)) |
| 58 | +>val : Symbol(val, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 14, 86)) |
| 59 | +>T : Symbol(T, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 14, 27)) |
| 60 | + |
| 61 | +// See type of `isWorking` - should include the type key as a union of strings |
| 62 | +const isWorking = createObjectGuard({ |
| 63 | +>isWorking : Symbol(isWorking, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 17, 5)) |
| 64 | +>createObjectGuard : Symbol(createObjectGuard, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 10, 1)) |
| 65 | + |
| 66 | +// ^? |
| 67 | + type: asLiteral('these', 'should', 'be', 'a', 'union'), |
| 68 | +>type : Symbol(type, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 17, 37)) |
| 69 | +>asLiteral : Symbol(asLiteral, Decl(stringLiteralFreshnessInGenericTypeGuard.ts, 12, 89)) |
| 70 | + |
| 71 | +}); |
| 72 | + |
| 73 | + |
0 commit comments