@stryke/types
Version:
A package containing many base TypeScript type definitions that are shared across many projects.
51 lines (50 loc) • 2 kB
text/typescript
import { Primitive } from "./base.mjs";
//#region src/utilities.d.ts
type Nullable<T> = T | null;
type IsNullable<T> = [null] extends [T] ? true : false;
type RequiredByKey<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
type NoInfer<T> = [T][T extends any ? 0 : never];
type Narrowable = string | number | bigint | boolean;
type NarrowRaw<A> = (A extends [] ? [] : never) | (A extends Narrowable ? A : never) | { [K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]> };
type Narrow<A> = Try<A, [], NarrowRaw<A>>;
type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch;
type Pretty<T> = { [K in keyof T]: T[K] } & {};
type ComputeRange<N extends number, Result extends unknown[] = []> = Result["length"] extends N ? Result : ComputeRange<N, [...Result, Result["length"]]>;
type Index40 = ComputeRange<40>[number];
/**
* A utility type for specifying a name/value pair.
*/
interface NameValuePair<TValue, TName = string> {
/**
* The name of the pair
*/
name: TName;
/**
* The value of the pair
*/
value: TValue;
}
/**
* Ask TS to re-check that `A1` extends `A2`.
* And if it fails, `A2` will be enforced anyway.
* Can also be used to add constraints on parameters.
*
* @example
* ```ts
* import { Cast } from '@stryke/types'
*
* type test0 = Cast<'42', string> // '42'
* type test1 = Cast<'42', number> // number
* ```
*
* @param A1 - to check against
* @param A2 - to cast to
* @returns `A1 | A2`
*/
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
type DeepReadonly<T> = T extends Primitive ? T : T extends Array<infer U> ? DeepReadonlyArray<U> : DeepReadonlyObject<T>;
type DeepReadonlyArray<T> = ReadonlyArray<DeepReadonly<T>>;
type DeepReadonlyObject<T> = { readonly [K in keyof T]: DeepReadonly<T[K]> };
//#endregion
export { Cast, ComputeRange, DeepReadonly, DeepReadonlyArray, DeepReadonlyObject, Index40, IsNullable, NameValuePair, Narrow, NoInfer, Nullable, Pretty, RequiredByKey, Try };
//# sourceMappingURL=utilities.d.mts.map