Skip to content

Commit

Permalink
fix: set-* types with index signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
som-sm committed Dec 19, 2024
1 parent 669ade8 commit 9d7471f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion source/set-optional.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {DistributedPick} from './distributed-pick';
import type {Except} from './except';
import type {KeysOfUnion} from './keys-of-union';
import type {Simplify} from './simplify';

/**
Expand Down Expand Up @@ -32,6 +34,6 @@ export type SetOptional<BaseType, Keys extends keyof BaseType> =
// Pick just the keys that are readonly from the base type.
Except<BaseType, Keys> &
// Pick the keys that should be mutable from the base type and make them mutable.
Partial<Except<BaseType, Exclude<keyof BaseType, Keys>>>
Partial<DistributedPick<BaseType, Keys & KeysOfUnion<BaseType>>>
>
: never;
4 changes: 3 additions & 1 deletion source/set-readonly.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {DistributedPick} from './distributed-pick';
import type {Except} from './except';
import type {KeysOfUnion} from './keys-of-union';
import type {Simplify} from './simplify';

/**
Expand Down Expand Up @@ -33,6 +35,6 @@ export type SetReadonly<BaseType, Keys extends keyof BaseType> =
BaseType extends unknown
? Simplify<
Except<BaseType, Keys> &
Readonly<Except<BaseType, Exclude<keyof BaseType, Keys>>>
Readonly<DistributedPick<BaseType, Keys & KeysOfUnion<BaseType>>>
>
: never;
4 changes: 3 additions & 1 deletion source/set-required.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {DistributedPick} from './distributed-pick';
import type {Except} from './except';
import type {KeysOfUnion} from './keys-of-union';
import type {Simplify} from './simplify';

/**
Expand Down Expand Up @@ -35,6 +37,6 @@ export type SetRequired<BaseType, Keys extends keyof BaseType> =
// Pick just the keys that are optional from the base type.
Except<BaseType, Keys> &
// Pick the keys that should be required from the base type and make them required.
Required<Except<BaseType, Exclude<keyof BaseType, Keys>>>
Required<DistributedPick<BaseType, Keys & KeysOfUnion<BaseType>>>
>
: never;
6 changes: 3 additions & 3 deletions test-d/distributed-pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ expectType<{readonly 'a': 1; 'b'?: 2; readonly 'c'?: 3}>(test1);
declare const test2: DistributedPick<{readonly 'a': 1; 'b'?: 2} | {readonly 'c'?: 3}, 'a' | 'b' | 'c'>;
expectType<{readonly 'a': 1; 'b'?: 2} | {readonly 'c'?: 3}>(test2);

// Picks all keys when second type argument is any
// Picks all keys, if `KeyType` is `any`
declare const test3: DistributedPick<{readonly 'a': 1; 'b'?: 2} | {readonly 'c'?: 3}, any>;
expectType<{readonly 'a': 1; 'b'?: 2} | {readonly 'c'?: 3}>(test3);

// Works with index signatures
declare const test4: DistributedPick<{[k: string]: unknown; a?: 1; b: '2'}, 'a' | 'b'>;
expectType<{a?: 1; b: '2'}>(test4);
declare const test4: DistributedPick<{[k: string]: unknown; a?: number; b: string}, 'a' | 'b'>;
expectType<{a?: number; b: string}>(test4);
4 changes: 4 additions & 0 deletions test-d/set-optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ expectType<{readonly a?: number; b?: string; c?: boolean}>(variation7);
// Does nothing, if `Keys` is `never`.
declare const variation8: SetOptional<{a?: number; readonly b?: string; readonly c: boolean}, never>;
expectType<{a?: number; readonly b?: string; readonly c: boolean}>(variation8);

// Works with index signatures
declare const variation9: SetOptional<{[k: string]: unknown; a: number; b?: string}, 'a' | 'b'>;
expectType<{[k: string]: unknown; a?: number; b?: string}>(variation9);
4 changes: 4 additions & 0 deletions test-d/set-readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ expectType<{readonly a?: number; readonly b: string; readonly c: boolean}>(varia
// Does nothing, if `Keys` is `never`.
declare const variation8: SetReadonly<{a: number; readonly b: string; readonly c: boolean}, never>;
expectType<{a: number; readonly b: string; readonly c: boolean}>(variation8);

// Works with index signatures
declare const variation9: SetReadonly<{[k: string]: unknown; a: number; readonly b: string}, 'a' | 'b'>;
expectType<{[k: string]: unknown; readonly a: number; readonly b: string}>(variation9);
4 changes: 4 additions & 0 deletions test-d/set-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ expectType<{readonly a: number; b: string; c: boolean}>(variation8);
// Does nothing, if `Keys` is `never`.
declare const variation9: SetRequired<{a?: number; readonly b?: string; readonly c: boolean}, never>;
expectType<{a?: number; readonly b?: string; readonly c: boolean}>(variation9);

// Works with index signatures
declare const variation10: SetRequired<{[k: string]: unknown; a?: number; b: string}, 'a' | 'b'>;
expectType<{[k: string]: unknown; a: number; b: string}>(variation10);

0 comments on commit 9d7471f

Please sign in to comment.