a33e1251a17aee350a9007a1388816d6ff90d7046d65a28ef0e8de8ac70af3a825070be22c3d287dc9b5982e0f9db8e94c4c6cdb88628537737d5908f76d35 914 B

123456789101112131415161718192021222324252627282930313233
  1. import {Except} from './except';
  2. import {Simplify} from './simplify';
  3. /**
  4. Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` 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 required.
  6. @example
  7. ```
  8. import {SetRequired} from 'type-fest';
  9. type Foo = {
  10. a?: number;
  11. b: string;
  12. c?: boolean;
  13. }
  14. type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
  15. // type SomeRequired = {
  16. // a?: number;
  17. // b: string; // Was already required and still is.
  18. // c: boolean; // Is now required.
  19. // }
  20. ```
  21. */
  22. export type SetRequired<BaseType, Keys extends keyof BaseType> =
  23. Simplify<
  24. // Pick just the keys that are optional from the base type.
  25. Except<BaseType, Keys> &
  26. // Pick the keys that should be required from the base type and make them required.
  27. Required<Pick<BaseType, Keys>>
  28. >;