Skip to content

Commit

Permalink
SetRequired: Fix it incorrectly combining individual key types (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekm1 committed May 2, 2023
1 parent 44ec7ec commit 9c12497
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 1 addition & 2 deletions source/set-required.d.ts
Expand Up @@ -28,8 +28,7 @@ type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
*/
export type SetRequired<BaseType, Keys extends keyof BaseType> =
Simplify<
// Pick just the keys that are optional from the base type.
Except<BaseType, Keys> &
BaseType &
// Pick the keys that should be required from the base type and make them required.
Required<Pick<BaseType, Keys>>
>;
4 changes: 4 additions & 0 deletions test-d/set-required.ts
Expand Up @@ -16,3 +16,7 @@ expectType<{a: number; b: string; c: boolean}>(variation3);
// Fail if type changes even if optional is right.
declare const variation4: SetRequired<{a?: number; b: string; c?: boolean}, 'b' | 'c'>;
expectNotAssignable<{a?: boolean; b: string; c: boolean}>(variation4);

// Update one required and one optional to required in a union.
declare const variation5: SetRequired<{a?: '1'; b: string; c?: boolean} | {a?: '2'; b: string; c?: boolean}, 'a' | 'b'>;
expectType<{a: '1'; b: string; c?: boolean} | {a: '2'; b: string; c?: boolean}>(variation5);

0 comments on commit 9c12497

Please sign in to comment.