|
| 1 | +import {expectType} from 'tsd'; |
| 2 | +import type {SetRequiredDeep} from '../index'; |
| 3 | + |
| 4 | +// Set nested key to required |
| 5 | +declare const variation1: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b.c'>; |
| 6 | +expectType<{a?: number; b?: {c: string}}>(variation1); |
| 7 | + |
| 8 | +// Set key to required but not nested keys if not specified |
| 9 | +declare const variation2: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b'>; |
| 10 | +expectType<{a?: number; b: {c?: string}}>(variation2); |
| 11 | + |
| 12 | +// Set root key to required |
| 13 | +declare const variation3: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'a'>; |
| 14 | +expectType<{a: number; b?: {c?: string}}>(variation3); |
| 15 | + |
| 16 | +// Keeps required key as required |
| 17 | +declare const variation4: SetRequiredDeep<{a: number; b?: {c?: string}}, 'a'>; |
| 18 | +expectType<{a: number; b?: {c?: string}}>(variation4); |
| 19 | + |
| 20 | +// Set key to required in a union. |
| 21 | +declare const variation5: SetRequiredDeep<{a?: '1'; b?: {c?: boolean}} | {a?: '2'; b?: {c?: boolean}}, 'a'>; |
| 22 | +expectType<{a: '1'; b?: {c?: boolean}} | {a: '2'; b?: {c?: boolean}}>(variation5); |
| 23 | + |
| 24 | +// Set array key to required |
| 25 | +declare const variation6: SetRequiredDeep<{a?: Array<{b?: number}>}, 'a'>; |
| 26 | +expectType<{a: Array<{b?: number}>}>(variation6); |
| 27 | + |
| 28 | +// Set key inside array to required |
| 29 | +declare const variation7: SetRequiredDeep<{a?: Array<{b?: number}>}, `a.${number}.b`>; |
| 30 | +expectType<{a?: Array<{b: number}>}>(variation7); |
| 31 | + |
| 32 | +// Set only specified keys inside array to required |
| 33 | +declare const variation8: SetRequiredDeep<{a?: Array<{b?: number; c?: string}>}, `a.${number}.b`>; |
| 34 | +expectType<{a?: Array<{b: number; c?: string}>}>(variation8); |
| 35 | + |
| 36 | +// Can set both root and nested keys to required |
| 37 | +declare const variation9: SetRequiredDeep<{a?: number; b?: {c?: string}}, 'b' | 'b.c'>; |
| 38 | +expectType<{a?: number; b: {c: string}}>(variation9); |
| 39 | + |
| 40 | +// Preserves required root keys |
| 41 | +declare const variation10: SetRequiredDeep<{a: 1; b: {c?: 1}}, 'b.c'>; |
| 42 | +expectType<{a: 1; b: {c: 1}}>(variation10); |
| 43 | + |
| 44 | +// Preserves union in root keys |
| 45 | +declare const variation11: SetRequiredDeep<{a: 1; b: {c?: 1} | number}, 'b.c'>; |
| 46 | +expectType<{a: 1; b: {c: 1} | number}>(variation11); |
| 47 | + |
| 48 | +// Preserves readonly in root keys |
| 49 | +declare const variation12: SetRequiredDeep<{a: 1; readonly b: {c?: 1}}, 'b.c'>; |
| 50 | +expectType<{a: 1; readonly b: {c: 1}}>(variation12); |
| 51 | + |
| 52 | +// Works with number keys |
| 53 | +declare const variation13: SetRequiredDeep<{0: 1; 1: {2?: string}}, '1.2'>; |
| 54 | +expectType<{0: 1; 1: {2: string}}>(variation13); |
| 55 | + |
| 56 | +// Multiple keys |
| 57 | +declare const variation14: SetRequiredDeep<{a?: 1; b?: {c?: 2}; d?: {e?: {f?: 2}; g?: 3}}, 'a' | 'b' | 'b.c' | 'd.e.f' | 'd.g'>; |
| 58 | +expectType<{a: 1; b: {c: 2}; d?: {e?: {f: 2}; g: 3}}>(variation14); |
| 59 | + |
| 60 | +// Index signatures |
| 61 | +declare const variation15: SetRequiredDeep<{[x: string]: any; a?: number; b?: {c?: number}}, 'a' | 'b.c'>; |
| 62 | +expectType<{[x: string]: any; a: number; b?: {c: number}}>(variation15); |
0 commit comments