@unblocks/types
Version:
Useful TypeScript types
28 lines (26 loc) • 1.28 kB
text/typescript
/** Get all values of a map type */
type ValueOf<T> = T[keyof T];
/** Get all possible keys from union types */
type KeysOfUnion<T> = T extends T ? keyof T : never;
/** Remove field with value "never" from a Map */
type RemoveNever<Map> = Pick<Map, {
[K in keyof Map]: Map[K] extends never ? never : K;
}[keyof Map]>;
/** Make fields partial and also recursively make nested fields partial */
type DeepPartial<T> = {
[K in keyof T]?: DeepPartial<T[K]>;
};
/** Make specified fields optional while leaving the remaining fields the same */
type PartialOnly<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
/** Make all fields optional while leaving the specified fields the same */
type PartialExcept<T, K extends keyof T> = Partial<Omit<T, K>> & Pick<T, K>;
/**
* Autocomplete in typescript of literal type and string
* https://stackoverflow.com/questions/74467392/autocomplete-in-typescript-of-literal-type-and-string/74467583#74467583
*/
type KnownString<T extends string> = T | (string & Record<never, never>);
/**
* Make a type or array of type
*/
type MaybeArray<T, IsArray extends boolean = boolean> = IsArray extends true ? T[] : T;
export type { DeepPartial, KeysOfUnion, KnownString, MaybeArray, PartialExcept, PartialOnly, RemoveNever, ValueOf };