|
| 1 | +import {expectType, expectError} from 'tsd'; |
| 2 | +import type {DistributedOmit, Except} from '../index'; |
| 3 | + |
| 4 | +// When passing a non-union type, and |
| 5 | +// omitting keys that are present in the type. |
| 6 | +// It behaves exactly like `Except`. |
| 7 | + |
| 8 | +type Example1 = { |
| 9 | + a: number; |
| 10 | + b: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type Actual1 = DistributedOmit<Example1, 'a'>; |
| 14 | +type Actual2 = DistributedOmit<Example1, 'b'>; |
| 15 | +type Actual3 = DistributedOmit<Example1, 'a' | 'b'>; |
| 16 | + |
| 17 | +type Expected1 = Except<Example1, 'a'>; |
| 18 | +type Expected2 = Except<Example1, 'b'>; |
| 19 | +type Expected3 = Except<Example1, 'a' | 'b'>; |
| 20 | + |
| 21 | +declare const expected1: Expected1; |
| 22 | +declare const expected2: Expected2; |
| 23 | +declare const expected3: Expected3; |
| 24 | + |
| 25 | +expectType<Actual1>(expected1); |
| 26 | +expectType<Actual2>(expected2); |
| 27 | +expectType<Actual3>(expected3); |
| 28 | + |
| 29 | +// When passing a non-union type, and |
| 30 | +// omitting keys that are NOT present in the type. |
| 31 | +// It behaves exactly like `Except`, by not letting you |
| 32 | +// omit keys that are not present in the type. |
| 33 | + |
| 34 | +type Example2 = { |
| 35 | + a: number; |
| 36 | + b: string; |
| 37 | +}; |
| 38 | + |
| 39 | +expectError(() => { |
| 40 | + type Actual4 = DistributedOmit<Example2, 'c'>; |
| 41 | +}); |
| 42 | + |
| 43 | +// When passing a union type, and |
| 44 | +// omitting keys that are present in some union members. |
| 45 | +// It lets you omit keys that are present in some union members, |
| 46 | +// and distributes over the union. |
| 47 | + |
| 48 | +type A = { |
| 49 | + discriminant: 'A'; |
| 50 | + foo: string; |
| 51 | + a: number; |
| 52 | +}; |
| 53 | + |
| 54 | +type B = { |
| 55 | + discriminant: 'B'; |
| 56 | + foo: string; |
| 57 | + bar: string; |
| 58 | + b: string; |
| 59 | +}; |
| 60 | + |
| 61 | +type C = { |
| 62 | + discriminant: 'C'; |
| 63 | + bar: string; |
| 64 | + c: boolean; |
| 65 | +}; |
| 66 | + |
| 67 | +type Union = A | B | C; |
| 68 | + |
| 69 | +type OmittedUnion = DistributedOmit<Union, 'foo' | 'bar'>; |
| 70 | + |
| 71 | +declare const omittedUnion: OmittedUnion; |
| 72 | + |
| 73 | +if (omittedUnion.discriminant === 'A') { |
| 74 | + expectType<{discriminant: 'A'; a: number}>(omittedUnion); |
| 75 | + expectError(omittedUnion.foo); |
| 76 | + expectError(omittedUnion.bar); |
| 77 | +} |
0 commit comments