|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {EmptyObject, UnwrapPartial} from '../index.d.ts'; |
| 3 | + |
| 4 | +type TestType = { |
| 5 | + a: string; |
| 6 | + b: number; |
| 7 | +}; |
| 8 | + |
| 9 | +expectType<TestType>({} as UnwrapPartial<Partial<TestType>>); |
| 10 | +expectType<TestType>({} as UnwrapPartial<{a?: string; b?: number}>); |
| 11 | +expectType<Partial<TestType>>({} as UnwrapPartial<Partial<Partial<TestType>>>); |
| 12 | + |
| 13 | +// `UnwrapPartial` preserves optional properties |
| 14 | +type TestTypeWithOptionalProp = TestType & { |
| 15 | + c?: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +type AnotherTestType = { |
| 19 | + c: boolean; |
| 20 | + d: 'literal'; |
| 21 | +}; |
| 22 | + |
| 23 | +type TestTypeWithOptionalProps = TestType & Partial<AnotherTestType>; |
| 24 | + |
| 25 | +expectType<TestTypeWithOptionalProp>({} as UnwrapPartial<Partial<TestTypeWithOptionalProp>>); |
| 26 | +expectType<TestTypeWithOptionalProps>({} as UnwrapPartial<Partial<TestTypeWithOptionalProps>>); |
| 27 | + |
| 28 | +// `UnwrapPartial` preserves nested `Partial` properties |
| 29 | +type TestTypeWithPartialProp = TestType & { |
| 30 | + c: Partial<TestType>; |
| 31 | +}; |
| 32 | + |
| 33 | +expectType<TestTypeWithPartialProp>({} as UnwrapPartial<Partial<TestTypeWithPartialProp>>); |
| 34 | + |
| 35 | +// `UnwrapPartial` preserves readonly properties |
| 36 | +type TestTypeWithReadonlyProps = Readonly<TestType> & { |
| 37 | + readonly c: boolean; |
| 38 | +}; |
| 39 | + |
| 40 | +expectType<TestTypeWithReadonlyProps>({} as UnwrapPartial<Partial<TestTypeWithReadonlyProps>>); |
| 41 | + |
| 42 | +// `UnwrapPartial` works with methods |
| 43 | +type TestTypeWithMethod = { |
| 44 | + c(): void; |
| 45 | +}; |
| 46 | + |
| 47 | +expectType<TestTypeWithMethod>({} as UnwrapPartial<Partial<TestTypeWithMethod>>); |
| 48 | + |
| 49 | +// `UnwrapPartial` works with union types |
| 50 | +type PartialUnionType = Partial<TestType> | Partial<AnotherTestType>; |
| 51 | + |
| 52 | +expectType<TestType | AnotherTestType>({} as UnwrapPartial<PartialUnionType>); |
| 53 | +expectType<TestType | AnotherTestType>({} as UnwrapPartial<Partial<TestType> | AnotherTestType>); |
| 54 | + |
| 55 | +// `UnwrapPartial` works with index signatures |
| 56 | +type ArrayLikeTestType = { |
| 57 | + [index: number]: string; |
| 58 | +}; |
| 59 | + |
| 60 | +type PlainObjectTestType = { |
| 61 | + readonly [key: string]: number | undefined; |
| 62 | +}; |
| 63 | + |
| 64 | +expectType<ArrayLikeTestType>({} as UnwrapPartial<Partial<ArrayLikeTestType>>); |
| 65 | +expectType<PlainObjectTestType>({} as UnwrapPartial<PlainObjectTestType>); |
| 66 | +expectType<Record<number, string>>({} as UnwrapPartial<Partial<Record<number, string>>>); |
| 67 | +expectType<string[]>({} as UnwrapPartial<Partial<string[]>>); |
| 68 | +expectType<Array<string | undefined>>({} as UnwrapPartial<Array<string | undefined>>); |
| 69 | + |
| 70 | +// `UnwrapPartial` works with tuples |
| 71 | +type TestTuple = [string, number, boolean]; |
| 72 | + |
| 73 | +expectType<TestTuple>({} as UnwrapPartial<Partial<TestTuple>>); |
| 74 | +expectType<TestTuple>({} as UnwrapPartial<[string?, number?, boolean?]>); |
| 75 | + |
| 76 | +// `UnwrapPartial` works with empty objects and unknown types |
| 77 | +expectType<EmptyObject>({} as UnwrapPartial<Partial<EmptyObject>>); |
| 78 | +expectType<unknown>({} as UnwrapPartial<Partial<unknown>>); |
| 79 | +expectType<any>({} as UnwrapPartial<Partial<any>>); |
| 80 | +expectType<Record<string, unknown>>({} as UnwrapPartial<Partial<Record<string, unknown>>>); |
| 81 | +expectType<Record<string, any>>({} as UnwrapPartial<Partial<Record<string, any>>>); |
0 commit comments