convex
Version:
Client for the Convex Cloud
47 lines (42 loc) • 1.21 kB
text/typescript
/**
* Common utilities for manipulating TypeScript types.
* @module
*/
/**
* Hack! This type causes TypeScript to simplify how it renders object types.
*
* It is functionally the identity for object types, but in practice it can
* simplify expressions like `A & B`.
*/
export type Expand<ObjectType extends Record<any, any>> =
ObjectType extends Record<any, any>
? {
[Key in keyof ObjectType]: ObjectType[Key];
}
: never;
/**
* An `Omit<>` type that:
* 1. Applies to each element of a union.
* 2. Preserves the index signature of the underlying type.
*/
export type BetterOmit<T, K extends keyof T> = {
[Property in keyof T as Property extends K ? never : Property]: T[Property];
};
/**
* From ObjectType, pick the properties that are assignable to T.
*/
export type PickByValue<ObjectType, T> = Pick<
ObjectType,
{
[Key in keyof ObjectType]: ObjectType[Key] extends T ? Key : never;
}[keyof ObjectType]
>;
/**
* Convert a union type like `A | B | C` into an intersection type like
* `A & B & C`.
*/
export type UnionToIntersection<UnionType> = (
UnionType extends any ? (k: UnionType) => void : never
) extends (k: infer I) => void
? I
: never;