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 ab827f9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 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;
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<{[x: string]: number; 'a': 1; 'b'?: 2}, 'a' | 'b'>;
expectType<{[x: string]: number; a?: 1; b?: 2}>(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<{[x: string]: number; 'a': 1; readonly 'b': 2}, 'a' | 'b'>;
expectType<{[x: string]: number; readonly a: 1; readonly b: 2}>(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<{[x: string]: number; 'a': 1; 'b'?: 2}, 'a' | 'b'>;
expectType<{[x: string]: number; a: 1; b: 2}>(variation10);

0 comments on commit ab827f9

Please sign in to comment.