Common Types
Utility types and helper functions for common TypeScript patterns.
Value Resolution Types
Types for lazy/deferred value resolution patterns.
TResolver / TAsyncResolver
type TResolver<T> = (...args: any[]) => T;
type TAsyncResolver<T> = (...args: any[]) => T | Promise<T>;Function types that resolve to a value. TAsyncResolver supports async functions.
TValueOrResolver / TValueOrAsyncResolver
type TValueOrResolver<T> = T | TResolver<T>;
type TValueOrAsyncResolver<T> = T | TAsyncResolver<T>;Union types allowing either a direct value or a resolver function.
Usage:
import { TValueOrAsyncResolver, resolveValueAsync } from '@venizia/ignis-helpers';
interface DatabaseConfig {
host: string;
port: number;
}
type ConfigOption = TValueOrAsyncResolver<DatabaseConfig>;
// Direct value
const config1: ConfigOption = { host: 'localhost', port: 5432 };
// Sync resolver
const config2: ConfigOption = () => ({ host: 'localhost', port: 5432 });
// Async resolver
const config3: ConfigOption = async () => {
const config = await fetchConfigFromVault();
return config;
};
// Resolve any of the above
const resolved = await resolveValueAsync(config3);resolveValue / resolveValueAsync
const resolveValue: <T>(valueOrResolver: TValueOrResolver<T>) => T;
const resolveValueAsync: <T>(valueOrResolver: TValueOrAsyncResolver<T>) => Promise<T>;Helper functions to resolve lazy values. They handle:
- Direct values: returned as-is
- Class constructors: returned as-is (not invoked)
- Resolver functions: invoked and result returned
Nullable Types
TNullable
type TNullable<T> = T | undefined | null;Makes a type nullable (can be undefined or null).
ValueOrPromise
type ValueOrPromise<T> = T | Promise<T>;A value that may or may not be wrapped in a Promise.
Class Types
TConstructor / TAbstractConstructor
type TConstructor<T> = new (...args: any[]) => T;
type TAbstractConstructor<T> = abstract new (...args: any[]) => T;Types representing class constructors.
TClass / TAbstractClass
type TClass<T> = TConstructor<T> & { [property: string]: any };
type TAbstractClass<T> = TAbstractConstructor<T> & { [property: string]: any };Class types with static properties.
TMixinTarget / TAbstractMixinTarget
type TMixinTarget<T> = TConstructor<{ [P in keyof T]: T[P] }>;
type TAbstractMixinTarget<T> = TAbstractConstructor<{ [P in keyof T]: T[P] }>;Types for mixin pattern targets.
Object Utility Types
ValueOf
type ValueOf<T> = T[keyof T];Extracts the value types from an object type.
ValueOptional / ValueOptionalExcept
type ValueOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type ValueOptionalExcept<T, K extends keyof T> = Pick<T, K> & Partial<Omit<T, K>>;Make specific keys optional while keeping others required, or vice versa.
TPrettify
type TPrettify<T> = { [K in keyof T]: T[K] } & {};Flattens intersection types for better IDE display.
Const Value Types
TStringConstValue / TNumberConstValue / TConstValue
type TStringConstValue<T extends TClass<any>> = Extract<ValueOf<T>, string>;
type TNumberConstValue<T extends TClass<any>> = Extract<ValueOf<T>, number>;
type TConstValue<T extends TClass<any>> = Extract<ValueOf<T>, string | number>;Extract constant value types from a class.
See Also
Related Concepts:
- Dependency Injection - DI types and patterns
- Repositories - Repository mixins use these types
Other Helpers:
- Helpers Index - All available helpers
References:
- Repository Mixins - Uses mixin types
- Utilities Index - Type utilities
Best Practices:
- Architectural Patterns - TypeScript patterns