@stryke/types
Version:
A package containing many base TypeScript type definitions that are shared across many projects.
73 lines (72 loc) • 1.78 kB
TypeScript
export type JsonObject = {
[Key in string]: JsonValue;
} & {
[Key in string]?: JsonValue | undefined;
};
/**
* Matches a JSON array.
*/
export type JsonArray = JsonValue[] | readonly JsonValue[];
/**
* Matches any valid JSON primitive value.
*/
export 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`.
*/
export 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.
*/
export 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'
* };
* ```
*/
export 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'
* ```
*/
export type StringKeyOf<BaseType> = `${Extract<keyof BaseType, string | number>}`;