Skip to content

Commit

Permalink
Refactor Except type to exclude keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Feb 23, 2020
1 parent 4b07d3a commit a88aa39
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion source/set-optional.d.ts
@@ -1,3 +1,5 @@
import {Except} from './except';

/**
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
Expand All @@ -23,7 +25,7 @@ type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
*/
export type SetOptional<BaseType, Keys extends keyof BaseType = keyof BaseType> =
// Pick just the keys that are not optional from the base type.
Pick<BaseType, Exclude<keyof BaseType, Keys>> &
Except<BaseType, Keys> &
// Pick the keys that should be optional from the base type and make them optional.
Partial<Pick<BaseType, Keys>> extends
// If `InferredType` extends the previous, then for each key, use the inferred type key.
Expand Down
4 changes: 3 additions & 1 deletion source/set-required.d.ts
@@ -1,3 +1,5 @@
import {Except} from './except';

/**
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
Expand All @@ -23,7 +25,7 @@ type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
*/
export type SetRequired<BaseType, Keys extends keyof BaseType = keyof BaseType> =
// Pick just the keys that are not required from the base type.
Pick<BaseType, Exclude<keyof BaseType, Keys>> &
Except<BaseType, Keys> &
// Pick the keys that should be required from the base type and make them required.
Required<Pick<BaseType, Keys>> extends
// If `InferredType` extends the previous, then for each key, use the inferred type key.
Expand Down

0 comments on commit a88aa39

Please sign in to comment.