import { isNull, isUndefined, isUnknown } from '..'; export * from './newtypes/infrastructure'; /** * Extracts the types of the values of the properties of T. * * @example * type A = valueof<{ a: number, b: string }>; * // ^? number | string */ export type valueof = T[keyof T]; /** * Improved `keyof` to support unions * @example * type A = $keyof<{ a: number, b: string } | { c: boolean }>; * // ^? "a" | "b" | "c" */ export type $keyof = [Type] extends [never] ? keyof Type : Type extends Type ? keyof Type : never; /** * Converts a union of object types into a single object type with keys * being the union of all keys and values being the union of all values. * * @example * type A = unionToIntersection<{ a: number, b: string } | { a: string, c: boolean }>; * // ^? { a: number | string, b: string, c: boolean } */ export type unionToIntersection = { [Key in $keyof]: Extract[Key]; }; /** * This can be used to assert that a certain type `T` is subtype of another type `U`. * util for cast keyof objects in generics as example * @example * type Result1 = cast; * // ^? Result1 = number * * type Result2 = cast<'a', string>; * // ^? Result2 = 'a' */ export type cast = T extends U ? T : U; /** * Allow avoid infer on generics when you predifine a type on other context. * * Normally this is usefull on advanced generics functions. */ export type noInfer = [T][waitFor extends any ? 0 : never]; /** * Avoid ejecution of caching when create a interface or type. */ export type waitFor = any extends argForWait ? type : never; /** * A utility type that substitutes a default type when the provided type is `unknown`, `undefined` or `null`. * @example * type A = defaultOnNullable; // Result: string * type B = defaultOnNullable; // Result: string * type C = defaultOnNullable; // Result: string * type D = defaultOnNullable; // Result: number */ export type defaultOnNullable = $if | isUndefined | isNull, { then: Default; else: Type; }>; declare global { interface Error<_ extends string = ''> { } interface WillThrow<_ extends string> { } /** A parsed JSON value. */ type json = string | number | boolean | null | json[] | { [key: string]: json; }; /** A JSON stringify-able value. */ type serializable = string | number | boolean | null | undefined | serializable[] | SerializableBySelf | SerializableByFn; } export interface SerializableByFn { toJSON(): serializable; } export type SerializableBySelf = { [K in keyof T as T[K] extends anyFunction ? never : K]: serializable; };