Skip to content

Commit

Permalink
Improve the handling of map options
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed May 21, 2024
1 parent 4b6c9c2 commit a471e7c
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 105 deletions.
25 changes: 25 additions & 0 deletions src/types/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Makes optional keys required and add the the undefined type.
*
* ```
* interface Test {
* foo: number;
* bar?: number;
* baz: number | undefined;
* }
*
* Complete<Test> {
* foo: number;
* bar: number | undefined;
* baz: number | undefined;
* }
*
* ```
*
* See https://medium.com/terria/typescript-transforming-optional-properties-to-required-properties-that-may-be-undefined-7482cb4e1585
*/

export type Complete<T> = {
[P in keyof Required<T>]: Pick<T, P> extends Required<Pick<T, P>> ? T[P] : (T[P] | undefined);
}

Loading

0 comments on commit a471e7c

Please sign in to comment.