/** * Casts a type (unless it's already narrower) * @example * // returns 'hey' * Cast<'hey', string> * @example * // returns string * Cast */ export declare type Cast = TType extends TBroadType ? TType : TBroadType; /** * Gets a subtype of a collection type (object, array, tuple) * @example * Get<{name: string, pronoums: ['they','them']}, 'pronoums'> //['they','them'] * * You can also get within a nested collection by passing a tuple * @example * Get<{user: { name: string, pronoums: ['they','them']}}, ['user', 'pronoums', 0]> //'they' */ export declare type Get = TKeys extends any[] ? TKeys extends [] ? TCollection extends undefined ? TFallback : TCollection : TKeys extends [infer K1, ...infer Kr] ? K1 extends keyof TCollection ? Get extends infer X ? Cast : never : TFallback : never : Get; export declare type AreEqual = (() => T extends B ? 1 : 0) extends () => T extends A ? 1 : 0 ? true : false; export declare type DoesExtend = A extends B ? true : false; export declare type IsPlainObject = Bool.And, Bool.Not>>; export declare namespace A { /** * Aliases for common generic types */ type Function = (...args: any[]) => any; type Tuple = T[] | [T]; type TupleOrUnit = T | Tuple; type Object = object; type String = string; type Number = number; type Universal = string | number | boolean | undefined | null | bigint | object | ((...a: any[]) => any); } export declare namespace Obj { /** * Object Type utilities */ type Assert = Cast; type DeepReadonly = { readonly [TKey in keyof TObj]: TObj[TKey] extends any ? TObj[TKey] extends A.Function ? TObj[TKey] : TObj[TKey] extends A.Object ? DeepReadonly : TObj[TKey] : never; }; type KeyWithValue = { [Key in keyof TObj]: TObj[Key] extends TValueType ? Key : never; }[keyof TObj]; type Mergify = { [Key in keyof TObj]: TObj[Key]; } & unknown; type Update = Mergify<{ [K in Union.Exclude]: A[K]; } & { [K in keyof B]: B[K]; }>; } export declare namespace List { /** * Array and Tuple Type utilities */ type Assert = Cast; type Concat = [...List.Assert, ...List.Assert]; type Pushed = [...List.Assert, TType]; type Popped = TTuple extends [] ? [] : TTuple extends [...infer Popped, any] ? Popped : never; type Pop = TTuple extends [] ? undefined : TTuple extends [...List.Popped, infer X] ? X : never; type Shifted = TTuple extends [] ? [] : TTuple extends [any, ...infer Shifted] ? Shifted : never; type Unshift = [X, ...List.Assert]; type Shift = TTuple extends [] ? undefined : TTuple extends [infer X, ...infer _] ? X : never; type ConcatAll = TTuples extends [] ? [] : TTuples extends [infer A] ? A : TTuples extends [infer A, infer B, ...infer X] ? ConcatAll<[List.Concat, ...X]> : never; type Join = TTuple extends [] ? "" : TTuple extends [infer H, ...infer T] ? T extends [] ? H : `${String.Assert}${String.Assert}${Join}` : string; type Includes = TTuple extends [] ? false : TValue extends Get ? true : false; type Filter = TTuple extends [] ? [] : TTuple extends [infer H, ...infer T] ? H extends TValue ? Filter : [H, ...Filter] : never; namespace Filter { const Out: unique symbol; export type Out = typeof Out; export {}; } } export declare namespace Union { /** * Union type utilities */ type IsUnit = [Union.Popped] extends [never] ? true : false; type Popped = Union.Exclude>; type Exclude = TUnion extends TValue ? never : TUnion; type Extract = TUnion extends TValue ? TUnion : never; type Pop = ToIntersection void : never> extends (x: infer P) => void ? P : never; type ToIntersection = (TUnion extends unknown ? (k: TUnion) => void : never) extends (k: infer I) => void ? I : never; type ToTuple = [TUnion] extends [never] ? [] : [...Union.ToTuple>, Union.Pop]; } export declare namespace Bool { /** * Boolean Type utilities */ type Not = B extends true ? false : true; type And = AreEqual>, true>; } export declare namespace String { /** * String type utilities */ type Assert = Cast; type IsLiteral = TType extends A.String ? (A.String extends TType ? false : true) : false; type DoesStartWith = TString extends TValue ? true : TString extends `${String.Assert}${infer _}` ? true : false; type DoesContain = TString extends TValue ? true : TString extends `${infer _}${String.Assert}${infer __}` ? true : false; type Split = TString extends `${infer H}${String.Assert}${infer T}` ? [H, ...Split] : [TString]; type Replace = TString extends `${infer P}${String.Assert}${infer S}` ? `${P}${String.Assert}${Replace}` : TString; type Shifted = TString extends `${infer _}${infer T}` ? T : ""; } export declare namespace Num { type Assert = Cast; type PositiveIntegers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; type NegativeIntegers = [ -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20 ]; type PositiveIntegersUnshifted = List.Unshift; type PositiveIntegersUnshiftedTwice = List.Unshift; type NegativeIntegersUnshifted = List.Unshift; type NegativeIntegersUnshiftedTwice = List.Unshift; type IsWhole = Bool.Not>; type IsNegative = String.DoesStartWith, "-">; type Negate = N extends 0 ? 0 : IsNegative extends true ? Num.FromString>> : Num.FromString<`-${Num.Assert}`>; type ToString = `${Num.Assert}`; type FromString = S extends "0" ? 0 : String.DoesStartWith extends false ? { [I in keyof PositiveIntegersUnshifted]: I extends S ? PositiveIntegersUnshifted[I] : never; }[keyof PositiveIntegersUnshifted] : { [I in keyof NegativeIntegersUnshifted]: `-${Num.Assert}` extends S ? NegativeIntegersUnshifted[I] : never; }[keyof NegativeIntegersUnshifted]; type Increment = IsNegative extends false ? Get : Get>; type Decrement = IsNegative extends false ? Get : Get>; }