UNPKG

@lokalise/api-contracts

Version:
42 lines (41 loc) 1.82 kB
/** * Flattens an intersection type into a single object type, making hover tooltips * show the fully-resolved shape instead of `A & B & C`. */ export type Prettify<T> = { [K in keyof T]: T[K]; } & {}; /** * Returns true when T is a union with more than one member. */ export type IsUnion<T, U = T> = (T extends unknown ? ([U] extends [T] ? 0 : 1) : never) extends 0 ? false : true; /** * Helper to prevent extra keys. If T has keys not in U, it forces an error. */ export type Exactly<T, U> = T & { [K in keyof T]: K extends keyof U ? T[K] : never; }; /** * Makes the keys K of T optional, leaving the rest unchanged. * (Same shape as `MayOmit` from `@lokalise/universal-ts-utils`.) */ export type MayOmit<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>; /** * Distributed `keyof`: the union of keys across all members of a union type. * (Plain `keyof` over a union yields only the keys common to every member.) */ export type KeysOfUnion<TUnion> = TUnion extends unknown ? keyof TUnion : never; /** * Like `Omit`, but distributes over unions: each union member is transformed * separately, preserving discriminated-union relationships between keys. * (Plain `Omit` collapses a union into a single object type of its common keys.) * * Keys are constrained to `KeysOfUnion` rather than `keyof TUnion`, so keys present * on only some union members can be omitted too — members without the key pass through unchanged. */ export type DistributiveOmit<TUnion, TKeys extends KeysOfUnion<TUnion>> = TUnion extends unknown ? Omit<TUnion, TKeys> : never; /** * Extracts a union of value types from an object type. * Optionally constrained to a subset of keys via ValueType. */ export type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];