cc00c8f6380f928317213d609351d018dece9317e1b400a045da0619886314aabc44a371393ff2cb03ec1f4a34b0978d2870e3ddf2988d256c1065632e1756 911 B

123456789101112131415161718192021222324252627282930313233
  1. import {Except} from './except';
  2. import {Simplify} from './simplify';
  3. /**
  4. Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
  5. Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.
  6. @example
  7. ```
  8. import {SetOptional} from 'type-fest';
  9. type Foo = {
  10. a: number;
  11. b?: string;
  12. c: boolean;
  13. }
  14. type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
  15. // type SomeOptional = {
  16. // a: number;
  17. // b?: string; // Was already optional and still is.
  18. // c?: boolean; // Is now optional.
  19. // }
  20. ```
  21. */
  22. export type SetOptional<BaseType, Keys extends keyof BaseType> =
  23. Simplify<
  24. // Pick just the keys that are readonly from the base type.
  25. Except<BaseType, Keys> &
  26. // Pick the keys that should be mutable from the base type and make them mutable.
  27. Partial<Pick<BaseType, Keys>>
  28. >;