object-migrations
Version:
Linear, in-memory migrations for versioned objects
37 lines (36 loc) • 992 B
TypeScript
/**
* Refers to the class (constructor/`new`-able) of a type. Don't use this to instantiate objects at
* runtime, because it doesn't preserve constructor parameter types.
*
* @example
* ```typescript
* class Thing {
* constructor(n: number, s: string) {}
* }
* function make<C>(c: Class<C>): C {
* return new c();
* // Valid TypeScript, but this would fail at runtime because the 2 parameters aren't provided.
* }
* const t = make(Thing);
* // t is of type Thing. Typing t correctly in APIs like this, is why Class<T> exists.
* ```
*/
export type Class<T> = Function & (new (...args: any) => T);
/**
* Union of all keys whose values are functions.
*
* @example
* ```typescript
* class Thing {
* s = '';
* f1() {}
* f2() {}
* }
* function doWithThing(t: Thing, name: FunctionNames<Thing>): void {
* // name is 'f1' | 'f2'
* }
* ```
*/
export type FunctionNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never;
}[keyof T];