@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
21 lines (20 loc) • 1 kB
TypeScript
/**
* Counts the number of unique keys in an object type. Note that a key of just `string` will count
* as 1.
*
* @category Object
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export type KeyCount<T> = UnionToTuple<keyof T>['length'];
/**
* This is not exported because its order is not stable but it's okay for our simple use case where
* we simply want to count the size of the union.
*/
type UnionToTuple<T> = UnionToIntersection<T extends any ? (t: T) => T : never> extends (args: any) => infer W ? [...UnionToTuple<Exclude<T, W>>, W] : [];
/**
* This is the version of UnionToIntersection from type-fest v4.3.3. In version 4.4.0 type-fest
* changed this type helper and it broke how we're using it.
*/
type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends (mergedIntersection: infer Intersection) => void ? Intersection : never;
export {};