|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {ExclusifyUnion} from '../source/exclusify-union.d.ts'; |
| 3 | +import type {MapsSetsOrArrays, NonRecursiveType} from '../source/internal/type.d.ts'; |
| 4 | + |
| 5 | +expectType<{a: string; b?: never} | {a?: never; b: number}>({} as ExclusifyUnion<{a: string} | {b: number}>); |
| 6 | +expectType<{a: string; b?: never; c?: never} | {a?: never; b: number; c?: never} | {a?: never; b?: never; c: boolean}>( |
| 7 | + {} as ExclusifyUnion<{a: string} | {b: number} | {c: boolean}>, |
| 8 | +); |
| 9 | +expectType<{a: string; b: number; c?: never; d?: never} | {a?: never; b?: never; c: string; d: number}>( |
| 10 | + {} as ExclusifyUnion<{a: string; b: number} | {c: string; d: number}>, |
| 11 | +); |
| 12 | +expectType< |
| 13 | + | {a: string; b?: never; c?: never; d?: never; e?: never; f?: never} |
| 14 | + | {a?: never; b: string; c: number; d?: never; e?: never; f?: never} |
| 15 | + | {a?: never; b?: never; c?: never; d: 1; e: 2; f: 3} |
| 16 | +>( |
| 17 | + {} as ExclusifyUnion<{a: string} | {b: string; c: number} | {d: 1; e: 2; f: 3}>, |
| 18 | +); |
| 19 | + |
| 20 | +// Single member union |
| 21 | +expectType<{a: string}>({} as ExclusifyUnion<{a: string}>); |
| 22 | +expectType<{a?: string; readonly b?: number}>({} as ExclusifyUnion<{a?: string; readonly b?: number}>); |
| 23 | + |
| 24 | +// Shared keys |
| 25 | +expectType<{a: string; b?: never} | {a: string; b: number}>({} as ExclusifyUnion<{a: string} | {a: string; b: number}>); |
| 26 | +expectType<{a: string; b?: never; c?: never} | {a: string; b: number; c?: never} | {a?: never; b: string; c: boolean}>( |
| 27 | + {} as ExclusifyUnion<{a: string} | {a: string; b: number} | {b: string; c: boolean}>, |
| 28 | +); |
| 29 | + |
| 30 | +// Already exclusive unions |
| 31 | +expectType<{a: string; b?: never} | {a?: never; b: number}>({} as ExclusifyUnion<{a: string; b?: never} | {a?: never; b: number}>); |
| 32 | +expectType<{a: string} | {a: number}>({} as ExclusifyUnion<{a: string} | {a: number}>); |
| 33 | + |
| 34 | +// Preserves property modifiers |
| 35 | +expectType<{a?: 1; readonly b: 2; readonly c?: 3; d?: never; e?: never} | {a?: never; b?: never; c?: never; d: 4; readonly e?: 5}>( |
| 36 | + {} as ExclusifyUnion<{a?: 1; readonly b: 2; readonly c?: 3} | {d: 4; readonly e?: 5}>, |
| 37 | +); |
| 38 | +expectType<{a?: string; readonly b: number} | {readonly a: string; b?: number}>( |
| 39 | + {} as ExclusifyUnion<{a?: string; readonly b: number} | {readonly a: string; b?: number}>, |
| 40 | +); |
| 41 | + |
| 42 | +// Non-recursive types |
| 43 | +expectType<Set<string> | Map<string, string>>({} as ExclusifyUnion<Set<string> | Map<string, string>>); |
| 44 | +expectType<WeakSet<{a: string}> | WeakMap<{a: string}, string>>({} as ExclusifyUnion<WeakSet<{a: string}> | WeakMap<{a: string}, string>>); |
| 45 | +expectType<string[] | Set<string>>({} as ExclusifyUnion<string[] | Set<string>>); |
| 46 | +expectType<NonRecursiveType>({} as ExclusifyUnion<NonRecursiveType>); |
| 47 | +expectType<MapsSetsOrArrays>({} as ExclusifyUnion<MapsSetsOrArrays>); |
| 48 | + |
| 49 | +// Mix of non-recursive and recursive types |
| 50 | +expectType<{a: string; b?: never} | {a: number; b: true} | undefined>({} as ExclusifyUnion<{a: string} | {a: number; b: true} | undefined>); |
| 51 | +expectType<Date | {DDMMYYYY: string; MMDDYYYY?: never} | {DDMMYYYY?: never; MMDDYYYY: string}>( |
| 52 | + {} as ExclusifyUnion<Date | {DDMMYYYY: string} | {MMDDYYYY: string}>, |
| 53 | +); |
| 54 | +expectType<RegExp | null | {foo: string; bar?: never; baz?: never} | {foo?: never; bar: number; baz: {qux: string}}>( |
| 55 | + {} as ExclusifyUnion<RegExp | null | {foo: string} | {bar: number; baz: {qux: string}}>, |
| 56 | +); |
| 57 | + |
| 58 | +// Practical test cases |
| 59 | +type FileConfig = {filePath: string}; |
| 60 | +type InlineConfig = {content: string}; |
| 61 | + |
| 62 | +type Config = ExclusifyUnion<FileConfig | InlineConfig>; |
| 63 | +//=> {filePath: string; content?: never} | {content: string; filePath?: never} |
| 64 | + |
| 65 | +declare function loadConfig(options: Config): void; |
| 66 | + |
| 67 | +// @ts-expect-error |
| 68 | +loadConfig({filePath: './config.json', content: '{ "name": "app" }'}); // Cannot provide both properties |
| 69 | +loadConfig({filePath: './config.json'}); // Ok |
| 70 | +loadConfig({content: '{ "name": "app" }'}); // Ok |
| 71 | + |
| 72 | +type CardPayment = {amount: number; cardNumber: string}; |
| 73 | +type PaypalPayment = {amount: number; paypalId: string}; |
| 74 | + |
| 75 | +function processPayment(payment: ExclusifyUnion<CardPayment | PaypalPayment>) { |
| 76 | + // Can access `cardNumber` or `paypalId` directly |
| 77 | + // And, the resulting type is also correctly `string` |
| 78 | + const details = payment.cardNumber ?? payment.paypalId; |
| 79 | + expectType<string>(details); |
| 80 | +} |
| 81 | + |
| 82 | +// Boundary types |
| 83 | +expectType<unknown>({} as ExclusifyUnion<unknown>); |
| 84 | +expectType<any>({} as ExclusifyUnion<any>); |
| 85 | +expectType<never>({} as ExclusifyUnion<never>); |
0 commit comments