@stryke/types
Version:
A package containing many base TypeScript type definitions that are shared across many projects.
70 lines • 1.89 kB
text/typescript
//#region src/json.d.ts
type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined };
/**
* Matches a JSON array.
*/
type JsonArray = JsonValue[] | readonly JsonValue[];
/**
* Matches any valid JSON primitive value.
*/
type JsonPrimitive = string | number | boolean | null;
/**
* Matches any valid JSON value.
*
* @see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.
*/
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
/**
* Matches a JSON pointer's path segments.
*
* @remarks
* These segments are used to navigate through the JSON object structure and point to a specific, referenced value.
*/
type JsonPointerPath = (string | number)[];
/**
* Create a type with the keys of the given type changed to `string` type.
*
* Use-case: Changing interface values to strings in order to use them in a form model.
*
* @example
* ```
* import type {Stringified} from 'type-fest';
*
* type Car = {
* model: string;
* speed: number;
* }
*
* const carForm: Stringified<Car> = {
* model: 'Foo',
* speed: '101'
* };
* ```
*/
type Stringified<ObjectType> = { [KeyType in keyof ObjectType]: string };
/**
* Get keys of the given type as strings.
*
* Number keys are converted to strings.
*
* Use-cases:
* - Get string keys from a type which may have number keys.
* - Makes it possible to index using strings retrieved from template types.
*
* @example
* ```
* import type {StringKeyOf} from 'type-fest';
*
* type Foo = {
* 1: number,
* stringKey: string,
* };
*
* type StringKeysOfFoo = StringKeyOf<Foo>;
* //=> '1' | 'stringKey'
* ```
*/
type StringKeyOf<BaseType> = `${Extract<keyof BaseType, string | number>}`;
//#endregion
export { JsonArray, JsonObject, JsonPointerPath, JsonPrimitive, JsonValue, StringKeyOf, Stringified };
//# sourceMappingURL=json.d.mts.map