|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {ExtractStrict} from '../source/extract-strict'; |
| 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 largeShirtSizes: ExtractStrict<ShirtSize, LargeShirtSize>; |
| 11 | +expectType<LargeShirtSize>(largeShirtSizes); |
| 12 | + |
| 13 | +declare const smallShirtSizes: ExtractStrict<ShirtSize, SmallShirtSize>; |
| 14 | +expectType<SmallShirtSize>(smallShirtSizes); |
| 15 | + |
| 16 | +// @ts-expect-error |
| 17 | +declare const allInvalidShirtSizes: ExtractStrict<ShirtSize, 'skyscraper-large' | 'atom-small'>; |
| 18 | + |
| 19 | +// @ts-expect-error |
| 20 | +declare const someInvalidShirtSizes: ExtractStrict<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 | +declare const foobarByA: ExtractStrict<Foobar, {a: string}>; |
| 40 | +expectType<Foobar>(foobarByA); |
| 41 | + |
| 42 | +declare const onlyFooByKind: ExtractStrict<Foobar, {kind: 'foo'}>; |
| 43 | +expectType<Foo>(onlyFooByKind); |
| 44 | + |
| 45 | +declare const onlyFooByB: ExtractStrict<Foobar, {b: string}>; |
| 46 | +expectType<Foo>(onlyFooByB); |
| 47 | + |
| 48 | +declare const onlyBarByC: ExtractStrict<Foobar, {c: boolean}>; |
| 49 | +expectType<Bar>(onlyBarByC); |
| 50 | + |
| 51 | +declare const foobarByUnionBC: ExtractStrict<Foobar, {b: string} | {c: boolean}>; |
| 52 | +expectType<Foobar>(foobarByUnionBC); |
| 53 | + |
| 54 | +// @ts-expect-error |
| 55 | +declare const invalidLoneField: ExtractStrict<Foobar, {d: string}>; |
| 56 | + |
| 57 | +// @ts-expect-error |
| 58 | +declare const invalidMixedFields: ExtractStrict<Foobar, {kind: 'foo'; d: string}>; |
| 59 | + |
| 60 | +// @ts-expect-error |
| 61 | +declare const undefinedField: ExtractStrict<Foobar, undefined>; |
| 62 | + |
| 63 | +// Primitives |
| 64 | +expectType<number>({} as ExtractStrict<string | number, number>); |
| 65 | +expectType<number | bigint>({} as ExtractStrict<string | number | bigint, number | bigint>); |
| 66 | +expectType<'bar' | 'baz'>({} as ExtractStrict<'foo' | 'bar' | 'baz', `b${string}`>); |
| 67 | + |
| 68 | +// @ts-expect-error |
| 69 | +type invalid1 = ExtractStrict<string | number | boolean, number | bigint>; |
| 70 | +// @ts-expect-error |
| 71 | +type invalid2 = ExtractStrict<string, Uppercase<string>>; |
| 72 | + |
| 73 | +// Optional and readonly modifiers |
| 74 | +expectType<{a: string; b: number}>({} as ExtractStrict<{a: string; b: number}, {a?: string}>); |
| 75 | +expectType<string[]>({} as ExtractStrict<string[], readonly string[]>); |
| 76 | + |
| 77 | +// @ts-expect-error |
| 78 | +type invalid3 = ExtractStrict<{a?: string; b: number}, {a: string}>; |
| 79 | +// @ts-expect-error |
| 80 | +type invalid4 = ExtractStrict<readonly string[], string[]>; |
| 81 | + |
| 82 | +// Index signatures |
| 83 | +expectType<{c: true; d: false}>( |
| 84 | + {} as ExtractStrict<{a: string; b: number} | {c: true; d: false}, Record<string, boolean>>, |
| 85 | +); |
| 86 | + |
| 87 | +// @ts-expect-error |
| 88 | +type invalid5 = ExtractStrict<{a: string; b: number} | {c: true; d: false}, Record<string, string>>; |
| 89 | + |
| 90 | +// `any` and `never` |
| 91 | +expectType<string | {a: string; b: number} | string[]>( |
| 92 | + {} as ExtractStrict<string | {a: string; b: number} | string[], any>, |
| 93 | +); |
| 94 | +expectType<never>( |
| 95 | + {} as ExtractStrict<string | {a: string; b: number} | string[], never>, |
| 96 | +); |
| 97 | + |
| 98 | +// Miscellaneous |
| 99 | +expectType<[number, number]>({} as ExtractStrict<[number, number] | {x: number; y: number}, unknown[]>); |
| 100 | +expectType<[number, number]>({} as ExtractStrict<[number, number] | [number, number, number], {length: 2}>); |
| 101 | +expectType<{data: string | string[]}>({} as ExtractStrict<string | string[] | {data: string | string[]}, {data: unknown}>); |
0 commit comments