A collection of types for Maybe in TypeScript.
MaybePromise<T>- Represents a value that may or may not be a promise. Useful for functions that can return either synchronous or asynchronous values.UnwrapPromise<T>- Extracts the inner type from a Promise, returning the unwrapped type if it's a promise, otherwise returns the original type.
Fn<R, P>- A function type with return typeRand parametersP(defaults to any).AsyncFn<R, P>- An asynchronous function type with return typeRand parametersP.MaybeFunction<T, P>- Represents either a valueTor a function that returnsTwith parametersP.MaybeFn<T, P>- Alias forMaybeFunction<T, P>.MaybeAsyncFunction<T, P, Promised>- Represents either a value, a function returning a promise, or a function that may return synchronously or asynchronously based on thePromisedflag.UnwrapFunction<T>- Extracts the return type from a function type.UnwrapAsyncFunction<T>- Extracts the return type from an async function or MaybeAsyncFunction type.
MaybeArray<T>- Represents either a single valueTor an array ofT.MaybeNumeric<T>- Represents either a number, bigint, or the typeT(defaults to string). Useful for numeric string conversions.Numeric- Union type ofnumber | bigint.MaybeDate- Represents a Date object, a timestamp number, or a date string.MaybeRegex- Represents either a RegExp object or a regex pattern string.MaybeFalsy- Union of all falsy values:false | null | undefined | "" | 0 | void.
Install package:
# npm
npm install maybe-types
# yarn
yarn add maybe-types
# pnpm
pnpm install maybe-typesImport:
import type { MaybePromise } from "maybe-types";
async function fn(arg: string): MaybePromise<string> {
if (arg === "foo") {
return "bar";
}
return Promise.resolve("baz");
}
async function getPromise(): Promise<string> {
return promise = await fn("foo");
}- Clone this repository
- Install latest LTS version of Node.js
- Enable Corepack using
corepack enable - Install dependencies using
pnpm install - Run interactive tests using
pnpm test
Made with 💛
Published under MIT License.