type-party
v0.7.3
Published
Extends `type-fest` with extra goodies.
Readme
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-partyUse 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 typeU. Preserves tuple structure and readonly properties.PreserveReadonly<T, V>— Utility that preserves thereadonlynature 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 thesatisfiesoperator. Returns typeTwhile ensuring it's assignable toU. 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 fromtype-party/runtime/dates.jsto 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>— LikeCollapseCases, but works recursively on nested object types within discriminated unions.
Function Types
AnyFunction— Type alias for any function:(...args: any) => any.CallSignature<T>— Extracts just the call signature from a function type that may have additional properties.Bind1<F, A0>,Bind2<F, A0, A1>,Bind3<F, A0, A1, A2>,Bind4<F, A0, A1, A2, A3>— Types for binding (partially applying) the first N arguments of a function, returning a new function type for the remaining arguments.
JSON Types
JSON— Represents valid JSON values: objects, arrays, strings, numbers, booleans, and null. Very similar to type-fest'sJsonValue, except that it doesn't allow keys withundefinedvalues.JSONWithUndefined— LikeJSON, 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 typeT. See explainer and usage examples.jsonParse,jsonStringify, andjsonStringifyUnstableare exported fromtype-party/runtime/nonempty.jsto support working with this type.
Math & Numeric Types
NumericRange<Min, Max>— Creates a union type of numbers fromMintoMax(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 removesundefinedfrom a type, useful for convertingJSONWithUndefinedtoJSON.
Object Key Manipulation
AllKeys<T>— Returns all keys from a union of object types, unlikekeyofwhich 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 usesDrainOuterGenericto 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 toany). This is sometimes needed when working with infinitely recursive types.
Type Mapping
PickEach<Union, Keys>— LikePick, but preserves union structure instead of merging picked properties across union members.ReplaceDeep<Type, Search, Replacement>— Recursively replaces all instances ofSearchtype withReplacementtype throughout a complex type structure.
