Type Helpers
Table of Contents
Overview
The Type Transform module is a collection of types and functions designed to enhance the capabilities of TypeScript by offering flexible type manipulation tools. These tools are especially useful in large TypeScript projects where complex type operations might be required.
This module includes:
- Type aliases for partial and full types
- Utilities for working with classes and function arguments
- Promise handling utilities
- Type assertion functions
Type Aliases
Partial Types
-
PartialAny<T>
: Creates a type with all properties ofT
set tooptional
and of typeany
.type PartialAny<T> = { [P in keyof T]?: any };
-
PartialCustom<T, S>
: Creates a type with all properties ofT
set tooptional
with custom typeS
.type PartialCustom<T, S = any> = { [P in keyof T]?: S };
-
PartialCustomWith<T, S, A>
: Combines custom partial typeS
with additional propertiesA
.type PartialCustomWith<T, S, A> = PartialCustom<T, S> & A;
Full Types
-
FullCustom<T, S>
: Converts all properties ofT
to be of typeS
.type FullCustom<T, S = any> = { [P in keyof T]: S };
-
FullRequire<T>
: Converts all properties ofT
to berequired
.type FullRequire<T> = { [P in keyof T]: T[P] };
Intersection and Exclusion
-
Intersect<A, B>
: Creates a type with the intersecting properties ofA
andB
.type Intersect<A, B> = { [P in keyof A & keyof B]: A[P] | B[P] };
-
NoExtra<T, U>
: Ensures no extra properties inU
compared toT
.type NoExtra<T, U extends T = T> = U & Impossible<Exclude<keyof U, keyof T>>;
-
Impossible<K>
: Creates a type that is impossible to instantiate.type Impossible<K extends keyof any> = { [P in K]: never };
Class and Template Types
-
Class<T>
: A type that represents a constructor ofT
.export interface Class<T> { new (...args): T; name: string; }
-
ClassStaticTemplate<T, StaticTemplate>
: Combines a class typeT
with static template properties.export type ClassStaticTemplate<T, StaticTemplate> = Class<T> & StaticTemplate;
Array and Function Types
-
Unshift<H, T>
: Adds an element to the start of a tuple typeT
.export type Unshift<H, T extends readonly any[]> = ((arg: H, ...argN: T) => void) extends (...r: infer R) => void ? R : never;
-
Push<T, V>
: Adds an element to the end of a tuple typeT
.export type Push<T extends readonly any[], V> = Unshift<any, T> extends infer A ? { [K in keyof A]: K extends keyof T ? T[K] : V } : never;
-
AddFunctionArg<F, ExtraParam>
: Adds an extra parameter to the functionF
.export type AddFunctionArg<F extends (...args) => any, ExtraParam> = (...args: Extract<Push<Parameters<F>, ExtraParam>, readonly any[]>) => ReturnType<F>;
Utility Functions
autoImplement
autoImplement<T>()
A utility function to automatically implement a class with default values.
export function autoImplement<T>(): new () => T {
return class {} as any;
}
punchGrab
punchGrab<T>(res: T): PromiseCollapse<T>
A utility to flatten nested promises until the underlying value is obtained.
export function punchGrab<T = any>(res: T): PromiseCollapse<T> {
return promise(async (resolve, reject) => {
try {
while ((res as any)?.then) {
(res as any) = await res;
}
return resolve(res);
} catch (e) {
return reject(e);
}
}) as any;
}
as
as<T>(v?: any)
A type assertion utility function to cast a value to a specific type.
export function as<T>(v?: any) {
return v as T;
}
Miscellaneous Types
-
PromiseCollapse<A0>
: Flattens nested promises into a single resolved value type.export type PromiseCollapse<A0> = A0 extends Promise<Promise<Promise<Promise<Promise<infer X>>>>> ? X : A0 extends Promise<Promise<Promise<Promise<infer X>>>> ? X : A0 extends Promise<Promise<Promise<infer X>>> ? X : A0 extends Promise<Promise<infer X>> ? X : A0 extends Promise<infer X> ? X : A0;
-
TaggedTemplateSelfChain<T, S>
: Represents a self-chaining tagged template.export type TaggedTemplateSelfChain<T, S extends any[] = any[]> = T & ((strArr: TemplateStringsArray, ...args: S) => TaggedTemplateSelfChain<T>);
-
configBoolean
: Represents a configuration option that can be a boolean or numeric boolean.export type configBoolean = true | false | 1 | 0;
-
required
: A constant representing a required type.export const required: RequiredType = true;