types-belt
Version:
A comprehensive collection of TypeScript utility types for building robust and maintainable applications
134 lines • 4.2 kB
TypeScript
/**
* Conditional utility types for TypeScript
* @module ConditionalTypes
*/
/**
* Checks if T is exactly U
* @template T - The type to check
* @template U - The type to compare against
* @example
* ```typescript
* type IsString = Is<string, string>; // true
* type IsNumber = Is<string, number>; // false
* ```
*/
export type Is<T, U> = T extends U ? U extends T ? true : false : false;
/**
* Checks if T is assignable to U
* @template T - The type to check
* @template U - The target type
* @example
* ```typescript
* type CanAssign = Extends<string, string | number>; // true
* type CannotAssign = Extends<string, number>; // false
* ```
*/
export type Extends<T, U> = T extends U ? true : false;
/**
* Returns T if condition is true, otherwise U
* @template Condition - The boolean condition
* @template T - The type to return if true
* @template U - The type to return if false
* @example
* ```typescript
* type Result = If<true, string, number>; // string
* type Result2 = If<false, string, number>; // number
* ```
*/
export type If<Condition extends boolean, T, U> = Condition extends true ? T : U;
/**
* Returns T if T is not never, otherwise U
* @template T - The primary type
* @template U - The fallback type
* @example
* ```typescript
* type Result = NotNever<string, number>; // string
* type Result2 = NotNever<never, number>; // number
* ```
*/
export type NotNever<T, U> = T extends never ? U : T;
/**
* Returns T if T is not null, otherwise U
* @template T - The primary type
* @template U - The fallback type
* @example
* ```typescript
* type Result = NotNull<string, number>; // string
* type Result2 = NotNull<null, number>; // number
* ```
*/
export type NotNull<T, U> = T extends null ? U : T;
/**
* Returns T if T is not undefined, otherwise U
* @template T - The primary type
* @template U - The fallback type
* @example
* ```typescript
* type Result = NotUndefined<string, number>; // string
* type Result2 = NotUndefined<undefined, number>; // number
* ```
*/
export type NotUndefined<T, U> = T extends undefined ? U : T;
/**
* Returns T if T is not null or undefined, otherwise U
* @template T - The primary type
* @template U - The fallback type
* @example
* ```typescript
* type Result = NotNullOrUndefined<string, number>; // string
* type Result2 = NotNullOrUndefined<null, number>; // number
* type Result3 = NotNullOrUndefined<undefined, number>; // number
* ```
*/
export type NotNullOrUndefined<T, U> = T extends null | undefined ? U : T;
/**
* Returns true if T is a union type, false otherwise
* @template T - The type to check
* @example
* ```typescript
* type IsUnion = IsUnion<string | number>; // true
* type IsNotUnion = IsUnion<string>; // false
* ```
*/
export type IsUnion<T> = T extends any ? [any] extends [T] ? false : true : false;
/**
* Returns true if T is an array type, false otherwise
* @template T - The type to check
* @example
* ```typescript
* type IsArray = IsArray<string[]>; // true
* type IsNotArray = IsArray<string>; // false
* ```
*/
export type IsArray<T> = T extends readonly any[] ? true : false;
/**
* Returns true if T is a tuple type, false otherwise
* @template T - The type to check
* @example
* ```typescript
* type IsTuple = IsTuple<[string, number]>; // true
* type IsNotTuple = IsTuple<string[]>; // false
* ```
*/
export type IsTuple<T> = T extends readonly any[] ? number extends T['length'] ? false : true : false;
/**
* Returns true if T is a function type, false otherwise
* @template T - The type to check
* @example
* ```typescript
* type IsFunction = IsFunction<() => void>; // true
* type IsNotFunction = IsFunction<string>; // false
* ```
*/
export type IsFunction<T> = T extends (...args: any) => any ? true : false;
/**
* Returns true if T is a primitive type, false otherwise
* @template T - The type to check
* @example
* ```typescript
* type IsPrimitive = IsPrimitive<string>; // true
* type IsNotPrimitive = IsPrimitive<object>; // false
* ```
*/
export type IsPrimitive<T> = T extends string | number | boolean | symbol | null | undefined ? true : false;
//# sourceMappingURL=conditional.d.ts.map