@stryke/types
Version:
A package containing many base TypeScript type definitions that are shared across many projects.
54 lines (53 loc) • 1.88 kB
TypeScript
import type { Primitive } from "./base";
export type Nullable<T> = T | null;
export type IsNullable<T> = [null] extends [T] ? true : false;
export type RequiredByKey<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
export 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]>;
};
export type Narrow<A> = Try<A, [], NarrowRaw<A>>;
export type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch;
export type Pretty<T> = {
[K in keyof T]: T[K];
} & {};
export type ComputeRange<N extends number, Result extends unknown[] = []> = Result["length"] extends N ? Result : ComputeRange<N, [...Result, Result["length"]]>;
export type Index40 = ComputeRange<40>[number];
/**
* A utility type for specifying a name/value pair.
*/
export 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`
*/
export type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
export type DeepReadonly<T> = T extends Primitive ? T : T extends Array<infer U> ? DeepReadonlyArray<U> : DeepReadonlyObject<T>;
export type DeepReadonlyArray<T> = ReadonlyArray<DeepReadonly<T>>;
export type DeepReadonlyObject<T> = {
readonly [K in keyof T]: DeepReadonly<T[K]>;
};
export {};