npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itsmeid/handy-utility-types

v1.0.0

Published

Unlock the power of TypeScript with a collection of handy utility types.

Downloads

3,297

Readme


handy-utility-types

Status Github NPM License


📝 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