|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {ExcludeStrict} from '../source/exclude-strict.js'; |
| 3 | + |
| 4 | +// Primitive union tests |
| 5 | + |
| 6 | +type ShirtSize = 'xxxl' | 'xxl' | 'xl' | 'l' | 'm' | 's' | 'xs' | 'xxs'; |
| 7 | +type LargeShirtSize = 'xxxl' | 'xxl' | 'xl' | 'l'; |
| 8 | +type SmallShirtSize = 's' | 'xs' | 'xxs'; |
| 9 | + |
| 10 | +declare const nonLargeShirtSizes: ExcludeStrict<ShirtSize, LargeShirtSize>; |
| 11 | +expectType<'m' | SmallShirtSize>(nonLargeShirtSizes); |
| 12 | + |
| 13 | +declare const nonSmallShirtSizes: ExcludeStrict<ShirtSize, SmallShirtSize>; |
| 14 | +expectType<LargeShirtSize | 'm'>(nonSmallShirtSizes); |
| 15 | + |
| 16 | +// @ts-expect-error |
| 17 | +declare const allInvalidShirtSizes: ExcludeStrict<ShirtSize, 'skyscraper-large' | 'atom-small'>; |
| 18 | + |
| 19 | +// @ts-expect-error |
| 20 | +declare const someInvalidShirtSizes: ExcludeStrict<ShirtSize, 'm' | 'atom-small'>; |
| 21 | + |
| 22 | +// Object union tests |
| 23 | + |
| 24 | +type Foo = { |
| 25 | + kind: 'foo'; |
| 26 | + a: string; |
| 27 | + b: string; |
| 28 | +}; |
| 29 | + |
| 30 | +type Bar = { |
| 31 | + kind: 'bar'; |
| 32 | + a: string; |
| 33 | + b: number; |
| 34 | + c: boolean; |
| 35 | +}; |
| 36 | + |
| 37 | +type Foobar = Foo | Bar; |
| 38 | + |
| 39 | +expectType<never>({} as ExcludeStrict<Foobar, {a: string}>); |
| 40 | +expectType<Bar>({} as ExcludeStrict<Foobar, {kind: 'foo'}>); |
| 41 | +expectType<Bar>({} as ExcludeStrict<Foobar, {b: string}>); |
| 42 | +expectType<Foo>({} as ExcludeStrict<Foobar, {c: boolean}>); |
| 43 | +expectType<never>({} as ExcludeStrict<Foobar, {b: string} | {c: boolean}>); |
| 44 | + |
| 45 | +// @ts-expect-error |
| 46 | +declare const invalidLoneField: ExcludeStrict<Foobar, {d: string}>; |
| 47 | + |
| 48 | +// @ts-expect-error |
| 49 | +declare const invalidMixedFields: ExcludeStrict<Foobar, {kind: 'foo'; d: string}>; |
| 50 | + |
| 51 | +// @ts-expect-error |
| 52 | +declare const undefinedField: ExcludeStrict<Foobar, undefined>; |
| 53 | + |
| 54 | +// Primitives |
| 55 | +expectType<number>({} as ExcludeStrict<string | number, string>); |
| 56 | +expectType<string>({} as ExcludeStrict<string | number | bigint, number | bigint>); |
| 57 | +expectType<'foo'>({} as ExcludeStrict<'foo' | 'bar' | 'baz', `b${string}`>); |
| 58 | + |
| 59 | +// @ts-expect-error |
| 60 | +type invalid1 = ExcludeStrict<string | number | boolean, number | bigint>; |
| 61 | +// @ts-expect-error |
| 62 | +type invalid2 = ExcludeStrict<string, Uppercase<string>>; |
| 63 | + |
| 64 | +// Optional and readonly modifiers |
| 65 | +expectType<never>({} as ExcludeStrict<{a: string; b: number}, {a?: string}>); |
| 66 | +expectType<{c: string; d: number}>({} as ExcludeStrict<{a: string; b: number} | {c: string; d: number}, {a?: string}>); |
| 67 | +expectType<never>({} as ExcludeStrict<string[], readonly string[]>); |
| 68 | + |
| 69 | +// @ts-expect-error |
| 70 | +type invalid3 = ExcludeStrict<{a?: string; b: number}, {a: string}>; |
| 71 | +// @ts-expect-error |
| 72 | +type invalid4 = ExcludeStrict<readonly string[], string[]>; |
| 73 | + |
| 74 | +// Index signatures |
| 75 | +expectType<{a: string; b: number}>( |
| 76 | + {} as ExcludeStrict<{a: string; b: number} | {c: true; d: false}, Record<string, boolean>>, |
| 77 | +); |
| 78 | + |
| 79 | +// @ts-expect-error |
| 80 | +type invalid5 = ExcludeStrict<{a: string; b: number} | {c: true; d: false}, Record<string, string>>; |
| 81 | + |
| 82 | +// `any` and `never` |
| 83 | +expectType<never>( |
| 84 | + {} as ExcludeStrict<string | {a: string; b: number} | string[], any>, |
| 85 | +); |
| 86 | +expectType<string | {a: string; b: number} | string[]>( |
| 87 | + {} as ExcludeStrict<string | {a: string; b: number} | string[], never>, |
| 88 | +); |
| 89 | + |
| 90 | +// Miscellaneous |
| 91 | +expectType<{x: number; y: number}>({} as ExcludeStrict<[number, number] | {x: number; y: number}, unknown[]>); |
| 92 | +expectType<[number, number, number]>({} as ExcludeStrict<[number, number] | [number, number, number], {length: 2}>); |
| 93 | +expectType<string | string[]>({} as ExcludeStrict<string | string[] | {data: string | string[]}, {data: unknown}>); |
0 commit comments