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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-options

v0.1.0

Published

functional programming types in typescript

Readme

Types

This document describes a set of TypeScript types used to represent the presence or absence of values, HTTP headers, and HTTP requests.

Option Types

None

Represents the absence of a value.

  • type (string): The type identifier indicating the absence of a value ("none").

Some<T>

Represents the presence of a value.

  • type (string): The type identifier indicating the presence of a value ("some").
  • value (T): The value wrapped by Some.

Option<T>

Represents the presence or absence of a value.

  • None | Some<T>: Either None or Some type.

HTTP Types

Headers

Represents HTTP headers as a key-value pair where both key and value are strings.

  • Record<string, string>

Request

Represents an HTTP request object.

  • url (string): The URL of the request.
  • headers (Headers): The headers of the request.
  • method (string, optional): The HTTP method of the request.
  • body (string, optional): The body of the request.

Partial Types

PartialOption<T>

Represents a partial option, where all properties of the Option type are optional.

  • Partial<Option<T>>

PartialSome<T>

Represents a partial Some, where all properties of the Some type are optional.

  • Partial<Some<T>>

PartialNone

Represents a partial None, where all properties of the None type are optional.

  • Partial<None>

Optional Record Types

OptionalRecord<T>

Represents an optional record where each value is of type Option<T>.

  • Record<string, Option<T>>

OptionalSomeRecord<T>

Represents an optional record where each value is of type Some<T>.

  • Record<string, Some<T>>

OptionalNoneRecord

Represents an optional record where each value is of type None.

  • Record<string, None>

OptionalPartialRecord<T>

Represents an optional record where each value is of type Partial<Option<T>>.

  • Record<string, Partial<Option<T>>>

Result

export type Result<T, E> = Ok | Err;

  • Represents the result of an operation that can either succeed with a value of type T or fail with an error of type E.
const result: Result<number, string> = { type: "ok", value: 42 };

OK Type

  • type Ok = { type: "ok"; value: T }; Creates an OK type that wraps the value.
const ok: Ok<number> = { type: "ok", value: 42 };

Error Type

  • type Err = { type: "err"; error: E }; Creates an error type that wraps the error value.
const error: Err<string> = { type: "err", error: "An error occurred" };

Methods - Optional Utilities Documentation

Creation Functions

some<T>(value: T): Some<T>

Creates a Some value wrapping the provided value.

  • value (T): The value to wrap in a Some.
const someValue = some(42);

none: None

Represents the absence of a value.

const noneValue = none;

Error Handling Functions

OptionalCatch<T>(fn: Function): Option<T>

Executes the provided function and wraps its result in Some if successful, otherwise returns None.

  • fn (Function): The function to call to get the value.
const result = OptionalCatch(() => {
  throw new Error("An error occurred");
});

optionalResolve<T>(promise: Promise<T>): Promise<Option<T>>

Executes the provided promise and wraps its result in Some if resolved successfully, otherwise returns None.

  • promise (Promise<T>): The promise to resolve.

Mapping and Transformation Functions

OptionalMap<T, U>(option: Option<T>, fn: Function): Option<U>

Maps the value of an Option using the provided function.

  • option (Option<T>): The Option to map.
  • fn (Function): The mapping function.
const result = OptionalMap(some(42), (value) => value * 2);

toOptional(fn: Function): Function

Converts a function into an Optional function, returning Some if successful, otherwise returns None.

  • fn (Function): The function to convert.
const optionalFunction = toOptional(() => 42);
const result = optionalFunction(); // Some(42)

Validation and Unwrapping Functions

optionalDefined: Function

Checks if the argument is defined (neither null nor undefined).

  • arg (unknown): The argument to check.
const result = optionalDefined(42); // Some(42)

unwrap<T>(option: Option<T>): T

Unwraps the value from the Option. Throws an error if the Option is None.

  • option (Option<T>): The Option to unwrap.
const value = unwrap(some(42)); // 42

unwrapOr<T>(opt: Option<T>, fallback: T): T

Unwraps the value from the Option or returns a fallback value if the Option is None.

  • opt (Option<T>): The Option to unwrap.
  • fallback (T): The fallback value to return if the Option is None.
const value = unwrapOr(some(42), 0); // 42

unwrapExpect<T>(opt: Option<T>, message: string): T

Unwraps the value from the Option or throws an error with the provided message if the Option is None.

  • opt (Option<T>): The Option to unwrap.
  • message (string): The message to include in the error if the Option is None.
const value = unwrapExpect(some(42), "Value is None"); // 42

HTTP Utilities

fetchWithOption<T>(request: Request): Promise<Option<T>>

Represents an HTTP request.

  • request (Request): The HTTP request object.

Returns: A promise resolving to a Some value containing the response data, or None if an error occurs.

const request: Request = {
  url: "https://api.example.com",
  headers: { "Content-Type": "application/json" },
  method: "GET",
};

const response = await fetchWithOption(request);
response.map((data) => console.log(data));

Types

  • Some<T>: Represents the presence of a value.
  • None: Represents the absence of a value.
  • Option<T>: Represents the presence or absence of a value.
  • Request: Represents an HTTP request object.