|
| 1 | +// From https://github.com/sindresorhus/type-fest |
| 2 | + |
| 3 | +// We want to avoid generating .d.ts files and to avoid a peer dependency on type-fest, |
| 4 | +// so we copy the relevant types here. |
| 5 | + |
| 6 | +// MIT License |
| 7 | +// Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com) |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 10 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 11 | + |
| 12 | +/** |
| 13 | +Matches a JSON object. |
| 14 | +
|
| 15 | +This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`. |
| 16 | +
|
| 17 | +@category JSON |
| 18 | +*/ |
| 19 | +export type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined }; |
| 20 | + |
| 21 | +/** |
| 22 | +Matches a JSON array. |
| 23 | +
|
| 24 | +@category JSON |
| 25 | +*/ |
| 26 | +export type JsonArray = JsonValue[] | readonly JsonValue[]; |
| 27 | + |
| 28 | +/** |
| 29 | +Matches any valid JSON primitive value. |
| 30 | +
|
| 31 | +@category JSON |
| 32 | +*/ |
| 33 | +export type JsonPrimitive = string | number | boolean | null; |
| 34 | + |
| 35 | +/** |
| 36 | +Matches any valid JSON value. |
| 37 | +
|
| 38 | +@see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`. |
| 39 | +
|
| 40 | +@category JSON |
| 41 | +*/ |
| 42 | +export type JsonValue = JsonPrimitive | JsonObject | JsonArray; |
| 43 | + |
| 44 | +/** |
| 45 | +Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type. |
| 46 | +
|
| 47 | +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. |
| 48 | +
|
| 49 | +@example |
| 50 | +``` |
| 51 | +import type {SetRequired} from 'type-fest'; |
| 52 | +
|
| 53 | +type Foo = { |
| 54 | + a?: number; |
| 55 | + b: string; |
| 56 | + c?: boolean; |
| 57 | +} |
| 58 | +
|
| 59 | +type SomeRequired = SetRequired<Foo, 'b' | 'c'>; |
| 60 | +// type SomeRequired = { |
| 61 | +// a?: number; |
| 62 | +// b: string; // Was already required and still is. |
| 63 | +// c: boolean; // Is now required. |
| 64 | +// } |
| 65 | +``` |
| 66 | +
|
| 67 | +@category Object |
| 68 | +*/ |
| 69 | +export type SetRequired<BaseType, Keys extends keyof BaseType> = |
| 70 | + // Pick just the keys that are optional from the base type. |
| 71 | + Except<BaseType, Keys> & |
| 72 | + // Pick the keys that should be required from the base type and make them required. |
| 73 | + Required<Pick<BaseType, Keys>>; |
0 commit comments