type-space

Typescript type utilities for typing better TypeScript types.

GPL-3.0 License

Downloads
486
Stars
2
Committers
3

๐Ÿ“ฆ Installation

Use your favorite package manager to install @cuppachino/type-space as a developer dependency.

@cuppachino/type-space

pnpm add -D @cuppachino/type-space
npm install --save-dev @cuppachino/type-space

๐Ÿ”Ž Quick Reference

The source code is fully tsdoc'd, so you can use your IDE's intellisense to reference examples and descriptions at any time. Click any type for more documentation.

๐ŸŽ Base Types

  • NumberLiteral: A stringified number literal.
  • Stringifiable: An alias for string | number | bigint | boolean | null | undefined.
  • UnknownArray: An alias for unknown[] | readonly unknown[],
    safer than any.
  • UnknownRecord: An alias for Record<PropertyKey, unknown>,
    safer than any or {}.

๐Ÿงฎ Arithmetic Types

  • Absolute: Coerces a number literal to a positive number of the same magnitude.
  • Add: Return the sum of two number literals.
  • Subtract: Return the difference between two number literals.

๐Ÿ”ข Numeric Types

  • IntoNumber: Coerce a NumberLike type to a number
  • IntoNumberLiteral: Coerce a NumberLike type to a NumberLiteral
  • IsInteger: A boolean type that is true if a number literal is an integer.
  • IsPositive: A boolean type that is true if a number literal is positive.
  • IsNegative: A boolean type that is true if a number literal is negative.
  • IsWhole: A boolean type that is true if a number literal is a whole number.
  • NumberLike: Coerce either a number or a NumberLiteral into a union between the two.
  • ParseNumberLiteral: Coerce a NumberLiteral type to a number

๐Ÿ’ญ String Types

  • Chars: Splits a string literal into a tuple of characters. Reads more clearly than Split in some cases.
  • ReverseString: Reverse a string literal.
  • Split: Splits a string literal into a tuple of characters, separated by the given delimiter.
  • SplitAt: Split a string literal into a tuple of two strings, separated by the given index, non-inclusive.
  • Stringify: Converts a type to a string literal type, if possible.
  • StringIncludes: A boolean type that is true if a string literal includes a given substring (โŠ†).
  • StringIncludesProper: A boolean type that is true if a string literal includes a given substring, and the substring is not the entire string (โŠ‚).

๐Ÿ“œ Tuple Types

  • CreateTuple: Generate a fixed-length tuple.
  • Flat: Recursively flatten a tuple up to a given depth.
  • IndexOf: Return a union of a tuple's indices.
  • Indices: Generate a tuple of a tuple's indices.
  • Join: Joins a tuple of strings into a single string, separated by a delimiter.
  • Length: Extract the length property from an array or
    tuple.
  • MergeAll: Merge all type members of a tuple into a
    single type.
  • PartitionKeys: Extract a union of keys for each member in a tuple into a new tuple; order is preserved.
  • PartitionValues: Create a tuple of value(s) for the given key(s) in each member of a tuple; order is preserved.
  • PartitionPick: Maps picked properties from each member of a tuple into a new tuple; order is preserved.
  • Reverse: Reverse the order of elements in a tuple type.

A la Array.prototype

sig

Action :: <Tuple> -> NewTuple
  • Pop: Remove the last element from a tuple. Does not return the removed element.
  • PopBy: Remove the last N elements from a tuple.
  • Push: Adds one element type to the end of a tuple. Does not return the new length of the tuple.
  • Shift: Remove the first element from a tuple type. Does not return the removed element.
  • ShiftBy: Remove the first N elements from a tuple.
  • Unshift: Adds one element type to the beginning of a tuple. Does not return the new length of the tuple.

๐Ÿงฐ Utility Types

  • Assert: Assert that a type is assignable to another type; shorthand for T extends U ? T : never.
  • Combine: Simplify a type by mapping over its properties.
  • KeyOf: Extract all keys from every member of a union type, unlike keyof which only preserves shared members' keys.
  • Mutable: Recursively removes the readonly modifier from all properties of a type.
  • PartialSome: Return a new type that allows the specified keys to be undefined.
  • PickAll: Extract properties from all members in a union, missing properties default to | undefined.
  • RequireSome: Return a new type requiring the selected keys.
  • Simplify: Simplify a type by mapping over its inferred properties - use when Combine cannot infer a deep type.
  • Subset: TypeScript equivalent of โŠ†.
  • UnionLiteral: Create a union from a literal and primitive type without losing the literal type.
  • UnionToIntersection: Create an intersection from all members of a union type.
  • UnionToTuple: Convert a union to a tuple type. The order is not guaranteed.
  • Zip: From a tuple of keys and a tuple of values, create a new record.

Extract

Extends

| sig

Extends :: <T, R = T> -> boolean
  • T is the type to check.
  • R is the type returned when T extends the name of the generic.
  • ExtendsFunction: Return a type if the
    given type extends a function or method.

I hope you have fun with these utilities. Thank you for using type-space!