typyx
v3.4.0
Published
Bullet-proof TS even more
Maintainers
Readme
typyx
Bullet-proof TypeScript even more.
Installation
npm
npm i -D typyxpnpm
pnpm i -D typyxRequires TypeScript v5.0+.
Documentation
Check out the full API reference for detailed usage examples and docs.
Types
Object shape, keys, and modifiers
Assign<Obj, ObjArr>- Copies all enumerable own properties from one target object to a source array of objects.FilterBy<Obj, P>- Filters keys from the object typeObjbased on a specified predicateP.Flip<Obj>- Flips the keys and values of an object typeObj.ImmutableKeys<Obj>- Retrieves thereadonlykeys from an object typeObj.Keys<T>- Retrieves the union of keys of a typeT.KeysOfUnion<T>- Extracts the union of keys from a union of object types.MakeOptional<T, K>- Makes the specified keysKoptional while preserving the original modifiers of all other keys.MakeRequired<T, K>- Makes the specified keysKrequired while preserving the original modifiers of all other keys.Methods<Obj>- Gets the literal names of keys that are methods in an object typeObj.MutableKeys<Obj>- Retrieves the mutable keys from an object typeObj.NonRequiredKeys<Obj>- Returns all non-required keys of an object typeObj.OmitByType<Obj, T>- Omits properties fromObjwhose types are assignable toT.OmitCommonKeys<Obj1, Obj2>- Omits any keys shared byObj1andObj2.OmitExactlyByType<Obj, T>- Omits properties fromObjwhose types exactly matchT.PartialExcept<T, P>- Makes all properties inToptional except those inP, which remain required.PickByType<Obj, T>- Picks properties fromObjwhose types are assignable toT.PickCommonKeys<Obj1, Obj2>- Gets the common keys between two object types.PickExactlyByType<Obj, T>- Picks properties fromObjwhose types exactly matchT.Properties<Obj>- Gets the literal names of keys that are non-method properties in an object typeObj.ReplaceKeys<Obj1, P, Obj2>- Replaces propertiesPinObj1with the corresponding properties fromObj2.RequiredKeys<Obj>- Gets the required keys of an object typeObj.Vals<Obj>- Gets the union of value types from an object type.
Deep object utilities
DeepAwaited<T>- Recursively resolves all nestedPromisetypes to their underlying values.DeepImmutable<Obj>- Recursively makes every property inObjreadonly.DeepMutable<Obj>- Recursively removesreadonlyfrom every property inObj.DeepNotRequired<Obj>- Recursively makes all properties optional.DeepOmit<Obj, P>- Recursively omits specified nested properties from an object based on predicateP.DeepPick<Obj, P>- Deeply picks properties from a nested object based on predicateP.DeepRequired<Obj>- Recursively makes all properties required.DeepToPrimitive<Obj>- Recursively transforms an object type into one whose properties are their primitive counterparts.Paths<Obj>- Generates all possible dot-separated key paths from a nested object type.Prune<T, N = NotIncluded>- Recursively omits properties of typeNfromT.
Union and composition utilities
ExclusiveUnion<T>- Creates a union type where each variant keeps its own required properties while excluding incompatible ones.NotAssignableTo<U, V>- Excludes all members ofUthat are assignable toV.UnionToIntersection<U>- Converts a union type into an intersection type.UnionToTuple<T>- Converts a union type into a tuple type.
Array and tuple utilities
Append<Arr, Item>- Adds an item to the end of a tuple.EitherOneOrMany<T>- Represents either a single value of typeTor an array ofT.Head<Arr>- Gets the first element of a tuple.IsArrayIncludesTypeof<Arr, T>- Checks whether an array typeArrincludes one or more members assignable toT.Last<Arr>- Gets the last element of a tuple.NonEmptyArray<T>- Represents an array containing at least one element of typeT.Pop<Arr>- Removes the last element of a tuple.Prepend<Arr, Item>- Adds an item to the start of a tuple.SizedTuple<T, N>- Creates a tuple of lengthNwhere each element is of typeT.Tail<Arr>- Removes the first element of a tuple.UniqueArray<T>- Creates a unique array type from an array typeT.Zip<L, L1>- Pairs elements from two tuples by index into a tuple of pairs.
String utilities
CapitalizeFirst<T>- Capitalizes the first character of a string literal type.EnforcedString<Prefix, Contains, Suffix>- Restricts a string using optional prefix, substring, and suffix constraints.EqualStrlen<S1, S2>- Checks whether two strings have the same length.FilledString<Obj>- Errors on an empty string literal''.NumerifyString<S>- Converts a string literal into aNumericwhen possible.StringEndsWith<S, E>- Checks whether a stringSends withE.StringStartsWith<S, St>- Checks whether a stringSstarts withSt.StringifyPrimitive<P>- Turns a primitive value type into its string representation.StrBetween<S, Min, Max>- Ensures a stringShas a length within[Min, Max].Strlen<S>- Computes the length of a stringS.StrMax<S, Max>- Ensures that a stringShas length less than or equal toMax.StrMin<S, Min>- Ensures that a stringShas length greater than or equal toMin.
Numeric utilities
Abs<N>- Gets the absolute value of aNumeric.EvenNumeric<T>- Represents an evenNumeric.Float<N>- Type representing a float.Integer<N>- Represents an integer.NegativeFloat<N>- Represents a negativeFloat<N>.NegativeFloatString<S>- Represents a negative float parsed from a string.NegativeInteger<N>- Represents a negativeInteger<N>.NegativeIntegerString<S>- Represents a negative integer parsed from a string.Numeric- Representsnumber | bigint.OddNumeric<T>- Represents an oddNumeric.PositiveFloat<N>- Represents a positiveFloat<N>.PositiveFloatString<S>- Represents a positive float parsed from a string.PositiveInteger<N>- Represents a positiveInteger<N>.PositiveIntegerString<S>- Represents a positive integer parsed from a string.PositiveRange<N, M>- Represents a range of positive integers fromNtoMinclusive.
Logic, comparison, and control flow
And<B1, B2>- LogicalANDbetween two boolean types.Equals<X, Y>- Checks if two types are exactly equal.Extends<T, U>- Evaluates whether typeTis assignable to typeU.If<C, Do, Else>- Resolves toDoifCistrue, otherwiseElse.IfEquals<T, P, Do, Else>- Resolves toDoifTequalsP, otherwiseElse.IfExtends<T, P, Do, Else>- Resolves toDoifTextendsP, otherwiseElse.Is<T, U>- Checks if two types are exactly identical.IsNot<T, U>- Checks if two types are not identical.Nand<B1, B2>- LogicalNANDbetween two boolean types.Not<B>- Negates a boolean type.Or<B1, B2>- LogicalORbetween two boolean types.Xnor<B1, B2>- LogicalXNORbetween two boolean types.Xor<B1, B2>- LogicalXORbetween two boolean types.
Type predicates and checkers
FalsyProperties<T>- Extracts falsy properties from an object typeT.IsBigInt<T>- Checks ifTis abigint.IsBoolean<T>- Checks ifTis aboolean.IsDeepImmutable<Obj>- Checks if all nested properties ofObjare immutable.IsDeepMutable<Obj>- Checks if all nested properties ofObjare mutable.IsDeepNotRequired<Obj>- Checks if all nested properties ofObjare optional.IsDeepRequired<Obj>- Checks if all nested properties ofObjare required.IsExactlyAny<T>- Checks ifTis exactlyany.IsExactlyBigInt<T>- Checks ifTis exactlybigint.IsExactlyNumber<T>- Checks ifTis exactlynumber.IsExactlyString<T>- Checks ifTis exactlystring.IsExactlySymbol<T>- Checks ifTis exactlysymbol.IsExactlyUnknown<T>- Checks ifTis exactlyunknown.IsFalsy<T>- Checks if a given typeTisFalsy.IsFloat<N>- Checks if a given type is aFloat<N>.IsFunction<T>- Checks if a given typeTis a function.IsInteger<N>- Checks if a given numeric type is anInteger<N>.IsNever<T>- Checks if a type resolves tonever.IsNewable<T>- Checks if a typeTisNewable.IsNullable<T>- Checks if a typeTisNullable.IsNumber<T>- Checks if a typeTis anumber.IsNumeric<T>- Checks if a typeTisNumeric.IsObject<T>- Checks if a typeTqualifies as an object.IsString<T>- Checks if a typeTis astring.IsSymbol<T>- Checks if a typeTis asymbol.IsTruthy<T>- Checks if a typeTresolves to a truthy value.TestType<T1, T2, Expected>- Tests whetherT1andT2match the expected relationship.TruthyProperties<T>- Extracts truthy properties from an object typeT.
Core, nullability, and misc utilities
EmptyObject- Represents an empty object-like shape.ExcludeNull<T>- Excludesnullfrom a typeT.ExcludeNullable<T>- ExcludesNullablefrom a typeT.ExcludeUndefined<T>- Excludesundefinedfrom a typeT.Falsy- Represents JavaScript falsy values.Maybe<T>- Represents a type that may beNullable.MaybeUndefined<T>- Represents a type that may beundefined.Newable- Represents constructor functions that can be invoked withnew.NewType<New, Base>- Creates a branded type derived from an existing base type.Nullable- Represents a type that can benullorundefined.Optional<T>- Represents a type that may benull, similar to Python'sOptionalor Rust'sOption.Primitive- Represents all JavaScript primitive types.Simplify<T>- Flattens and normalizes a type for better readability.
The best way to understand how these types work is to check the tests directory.
Changelog
See releases.
License
MIT © @rccyx
