@itsmeid/handy-utility-types
v1.0.0
Published
Unlock the power of TypeScript with a collection of handy utility types.
Downloads
3,228
Maintainers
Readme
handy-utility-types
📝 Table of Contents
🤔 About
This package offers a suite of versatile utility types that simplify common tasks, improve type safety, and boost productivity.
- Easy to use
- Zero third party dependencies
- Type level only
- No more copy paste "type definitions" between projects
🔌 Installation
# NPM
npm install --save-dev @itsmeid/handy-utility-types
# BUN
bun add -d @itsmeid/handy-utility-types
⛏️ Built Using
- Typescript Strongly typed programming language that builds on JavaScript.
- Bun All-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager.
📔 Docs
Type to bypass any lint or type coverage checks. Please use only in circumstances that require it. See this link.
Remarks
Basically the same as built-in any from typescript. Do not use carelessly
Type representing a function that takes specified argument types and returns a specified type.
Example
type MyFunction = Fn<[number, string], boolean>;
// MyFunction is (arg1: number, arg2: string) => boolean
Type representing a class constructor that takes specified argument types and returns an instance of a specified type.
Example
type MyClass = Class<[number, string], TempClass>;
// MyClass is a constructor function that takes (arg1: number, arg2: string) and returns an instance of TempClass
Type that allows a value to be T, null, or undefined.
Example
const example: Nullable<string> = null; // valid
Type that allows a value to be T or null, but not undefined.
Example
const example: NullableStrict<string> = null; // valid
Extended TypeScript Record to define a generic object type.
Example
const obj: ObjectGeneric<number> = { a: 1, b: 2 };
Extended TypeScript Required to enforce specific properties as required.
Example
type MyType = RequiredProps<{ a?: number; b?: string }, 'a'>;
const example: MyType = { a: 1, b: 'test' }; // 'a' is required
Extended TypeScript Partial to make specific properties optional.
Example
type MyType = PartialProps<{ a: number; b: string }, 'a'>;
const example: MyType = { b: 'test' }; // 'a' is optional
Get the required keys from an object or record type.
Example
type MyType = { a: number; b?: string; c: boolean };
type ObjectRequiredKeysType = ObjectRequiredKeys<MyType>; // "a" | "c"
Get the optional keys from an object or record type.
Example
type MyType = { a: number; b?: string; c?: boolean };
type ObjectOptionalKeysType = ObjectOptionalKeys<MyType>; // "b" | "c"
Get all of the paths from an object T as a string union.
Example
type MyType = { a: { b: number }; c: string };
type Paths = ObjectPath<MyType>; // "a" | "a.b" | "c"
Get the type of a property based on a given path.
Example
type MyType = { a: { b: number }; c: string };
type ValueType = ObjectPathValue<MyType, 'a.b'>; // number
Combines two types by overwriting properties in T with those in U.
Example
type Original = { a: number; b: string; c: boolean };
type Updates = { b: number; c?: string };
type Result = Overwrite<Original, Updates>;
// Result is { a: number; b: number; c?: string }
Check the type equality between A and B.
Example
type IsEqualExample = IsEqual<string, string>; // true
Check the type inequality between A and B.
Example
type IsNotEqualExample = IsNotEqual<string, number>; // true
Checks if a type is a plain object literal (not an array or a function).
Example
type MyType = { a: number; b: string };
type NotObject = string[];
type IsMyTypeObjectLiteral = IsObjectLiteral<MyType>; // true
type IsNotObjectLiteral = IsObjectLiteral<NotObject>; // false
Checks if a type is an array.
Example
type IsMyTypeArray = IsArray<number[]>; // true
type IsNotArray = IsArray<string>; // false
A utility type that determines if a given type T
is a Promise.
Example
type IsMyTypePromise = IsPromise<Promise<number>>; // true
type IsNotPromise = IsPromise<string>; // false
A utility type that checks if a given type T
is a function.
Example
type IsMyTypeFunction = IsFunction<() => void>; // true
type IsNotFunction = IsFunction<string>; // false
A utility type that checks if a given type T
is a class.
Example
class MyClass {}
type IsMyTypeClass = IsClass<MyClass>; // true
type IsNotClass = IsClass<string>; // false
Extract the value type from an array type.
Example
type ValueType = ExtractArray<number[]>; // number
Extract the value type from a promise type.
Example
type ValueType = ExtractPromise<Promise<number>>; // number
Excludes all optional keys from an object type.
Example
type MyType = { a: number; b?: string; c: boolean; d?: undefined };
type RequiredOnly = ExcludeOptional<MyType>;
// RequiredOnly is { a: number; c: boolean }
Recursively excludes all optional keys from an object type.
Example
type MyType = {
a: number;
b?: string;
c: { d: boolean; e?: number };
f?: { g: string; h?: string }
};
type RequiredOnly = ExcludeOptionalDeep<MyType>;
// RequiredOnly is { a: number; c: { d: boolean } }
Excludes all required keys from an object type.
Example
type MyType = { a: number; b?: string; c: boolean };
type OptionalOnly = ExcludeRequired<MyType>;
// OptionalOnly is { b?: string }
Recursively excludes all required keys from an object type.
Example
type MyType = {
a: number;
b?: string;
c: { d: boolean; e?: number };
f?: { g: string; h?: string }[];
i: { j: string; k?: string }
};
type OptionalOnly = ExcludeRequiredDeep<MyType>;
// OptionalOnly is { b?: string; c: { e?: number }; f?: { g: string; h?: string }[]; i: { k?: string } }
Exclude null and undefined types from T excluding null and undefined.
Example
type NonNullableType = ExcludeNullable<string | null | undefined>; // string