1.0.0 • Published 9 months ago

@itsmeid/handy-utility-types v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago


📝 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

BypassAny

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

Fn

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

Class

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

Nullable

Type that allows a value to be T, null, or undefined.

Example

const example: Nullable<string> = null; // valid

NullableStrict

Type that allows a value to be T or null, but not undefined.

Example

const example: NullableStrict<string> = null; // valid

ObjectGeneric

Extended TypeScript Record to define a generic object type.

Example

const obj: ObjectGeneric<number> = { a: 1, b: 2 };

RequiredProps

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

PartialProps

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

ObjectRequiredKeys

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"

ObjectOptionalKeys

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"

ObjectPath

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"

ObjectPathValue

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

ObjectOverwrite

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 }

IsEqual

Check the type equality between A and B.

Example

type IsEqualExample = IsEqual<string, string>; // true

IsNotEqual

Check the type inequality between A and B.

Example

type IsNotEqualExample = IsNotEqual<string, number>; // true

IsObjectLiteral

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

IsArray

Checks if a type is an array.

Example

type IsMyTypeArray = IsArray<number[]>; // true
type IsNotArray = IsArray<string>; // false

IsPromise

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

IsFunction

A utility type that checks if a given type T is a function.

Example

type IsMyTypeFunction = IsFunction<() => void>; // true
type IsNotFunction = IsFunction<string>; // false

IsClass

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

ExtractArray

Extract the value type from an array type.

Example

type ValueType = ExtractArray<number[]>; // number

ExtractPromise

Extract the value type from a promise type.

Example

type ValueType = ExtractPromise<Promise<number>>; // number

ExcludeOptional

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 }

ExcludeOptionalDeep

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 } }

ExcludeRequired

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 }

ExcludeRequiredDeep

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 } }

ExcludeNullable

Exclude null and undefined types from T excluding null and undefined.

Example

type NonNullableType = ExcludeNullable<string | null | undefined>; // string