Skip to content

ethanresnick/type-party

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

type-party

Extends type-fest with extra goodies.

Mostly contains pure TS types, but also includes some type predicates and runtime utilities -- although those are all exported under separate entrypoints, so won't be loaded by default.

Install

npm install type-party

Use as:

// To get a pure type
import type { XXXX } from "type-party";

// To get one of the runtime exports.
// See package.json for the available /runtime/xyz.js export paths.
import { XXX } from "type-party/runtime/json.js"

Types

Array & Tuple Types

  • Filter<T, U> — Filters an array or tuple type, keeping only elements that extend type U. Preserves tuple structure and readonly properties.

  • PreserveReadonly<T, V> — Utility that preserves the readonly nature of an array type when transforming it to another array type. This simplifies building utility types that transform array types.

Assignability & Type Safety

  • Satisfies<T, U> — Type-level equivalent of the satisfies operator. Returns type T while ensuring it's assignable to U. This can be very useful for keeping types in sync or setting up various type-level "alarms" for when some expected invariant gets broken.

  • SatisfiedBy<T, U> — Returns the first type parameter while enforcing that it's a supertype of the second parameter.

Date Types

  • DateString — Tagged string type for date strings, useful for distinguishing dates from arbitrary strings in JSON contexts. Import from type-party/runtime/dates.js to get the relevant functions for creating/parsing these.

Design by Contract

  • PublicInterface<T> — Extracts only the public methods and fields from a class type. Useful for dependency injection where mocks need to satisfy the interface without private implementation details.

  • PublicMethodNames<T> — Extracts the public method names from a type, returning a union of string literal types.

Discriminated Union Helpers

  • CollapseCases<T> — Takes a union of object types and returns a single object type where each key's type is the union of types for that key across all cases. This is useful in cases where TS can't correctly figure out the assignability of complex union types. See the definition for examples.

  • CollapseCasesDeep<T> — Like CollapseCases, but works recursively on nested object types within discriminated unions.

Function Types

JSON Types

  • JSON — Represents valid JSON values: objects, arrays, strings, numbers, booleans, and null. Very similar to type-fest's JsonValue, except that it doesn't allow keys with undefined values.

  • JSONWithUndefined — Like JSON, but allows types with optional keys and undefined values that get omitted during JSON serialization.

  • JsonOf<T> — Tagged string type representing a JSON serialization of type T. See explainer and usage examples. jsonParse, jsonStringify, and jsonStringifyUnstable are exported from type-party/runtime/nonempty.js to support working with this type.

Math & Numeric Types

  • NumericRange<Min, Max> — Creates a union type of numbers from Min to Max (inclusive).

  • Permutations<T> — Generates all possible permutations of a tuple type using tuple length arithmetic.

Non-empty Types

  • NonEmptyString — Tagged string type that represents a non-empty string.

  • NonEmptyArray<T> — Array type that guarantees at least one element: [T, ...T[]].

isNonEmptyString, isNonEmptyArray, and mapNonEmpty are exported from type-party/runtime/nonempty.js to simplify working with these types.

Nullish Handling

  • RemoveUndefinedDeep<T> — Recursively removes undefined from a type, useful for converting JSONWithUndefined to JSON.

Object Key Manipulation

  • AllKeys<T> — Returns all keys from a union of object types, unlike keyof which only returns keys present in every union member.

  • StringKeys<T> — Filters object keys to only string keys.

  • NullableKeys<T> — Returns keys from an object type where the value can be null.

  • NarrowKeys<T, K, V> — Narrows specific keys in an object type to a more specific value type when there's type compatibility.

Type Simplification

  • Simplify<T> — Flattens intersection types into a single object type for better IDE display. Similar to type-fest's simplify, but uses DrainOuterGeneric to avoid "type instantiation excessively deep" errors.

  • DrainOuterGeneric<T> — Prevents type computations from contributing to TypeScript's instantiation depth counter, helping avoid "excessively deep" errors.

  • DepthCapped<T, DepthLimit?, AtDepthLimitType?> — Limits the nesting depth of recursive types to prevent TypeScript compiler performance issues. When depth limit is reached, deeper structures are replaced with the specified type (defaults to any). This is sometimes needed when working with infinitely recursive types.

Type Mapping

About

TypeScript types to simplify writing highly-type-safe code and doing type-level programming.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published