@cuppachino/type-space
v2.1.1
Published
A collection of type utilities for TypeScript.
Downloads
553
Maintainers
Readme
📦 Installation
Use your favorite package manager to install @cuppachino/type-space
as a developer dependency.
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 forstring | number | bigint | boolean | null | undefined
.UnknownArray
: An alias forunknown[] | readonly unknown[]
, safer thanany
.UnknownRecord
: An alias forRecord<PropertyKey, unknown>
, safer thanany
or{}
.
🧮 Arithmetic Types
Absolute
: Coerces a number literal to a positivenumber
of the same magnitude.Add
: Return the sum of two number literals.Subtract
: Return the difference between two number literals.
🔢 Numeric Types
IntoNumber
: Coerce aNumberLike
type to anumber
IntoNumberLiteral
: Coerce aNumberLike
type to aNumberLiteral
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 anumber
or aNumberLiteral
into a union between the two.ParseNumberLiteral
: Coerce aNumberLiteral
type to anumber
💭 String Types
Chars
: Splits a string literal into a tuple of characters. Reads more clearly thanSplit
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 lastN
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 firstN
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 forT extends U ? T : never
.Combine
: Simplify a type by mapping over its properties.KeyOf
: Extract all keys from every member of a union type, unlikekeyof
which only preserves shared members' keys.Mutable
: Recursively removes thereadonly
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 whenCombine
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
ExtractRequired
: Extract all non-optional properties from a type; ℹ️exactOptionalPropertyTypes.ExtractOptional
: Extract all optional properties from a type; ℹ️exactOptionalPropertyTypesExtractFunctions
: Create a new type of all property functions and methods in a type.ExtractSetMembers
: Create a union type of members in aSet
.
Extends
| sig
Extends :: <T, R = T> -> boolean
T
is the type to check.R
is the type returned whenT
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
!