ts-roids
v2.8.0
Published
Bullet-proof TS even more
Maintainers
Readme
ts-roids
Bullet proof TypeScript even more.
Installation
npm
npm i -D ts-roidspnpm
pnpm i -D ts-roidsRequires TypesScript v5.0+
Documentation
Checkout the full API reference for all usage examples with details.
Types
ExclusiveUnion<T>- Creates a union type where each variant has its specific properties required, while other properties are optional.UniqueArray<T>- Create unique array type from a given array typeT, a set perse.Prune<T,N = NotIncluded>- Prune a typeTby recursively omitting properties of typeN(defaults toNotIncluded).PositiveRange<N,M>- Represents a range of positive integers from N to M (inclusive), useful for enforcing numeric bounds.UnionToTuple<T>- Converts a union typeTinto a tuple type, allowing for ordered access to the union's members.EnforcedString<Prefix, Contains, Suffix>- Constructs a type that restricts a string based on three optional constraints: a prefix, a substring (must be contained), and a suffix.FilledString<Obj>- Error out on an empty ('') string.DeepToPrimitive<Obj>- Recursively transforms an object type T into a type where all properties are replaced with their corresponding primitive types.Assign<Obj,ObjArr>- Copies all enumerable own properties from one target object to a source array of objects.CapitalizeFirst<T>- Capitalizes the first character of a string literal type while preserving the rest.Is<T,U>- Checks if two typesTandUare exactly identical and resolves to true or false at the type level.IsNot<T,U>- Check ifTandUare not the same.Paths<Obj- Generates all possible dot-separated key paths from a nested object type.NonEmptyArray<T>- The array type must contain at least one element of typeT.DeepAwaited<T>- Recursively resolves all nestedPromisetypes to their underlying value.Flip<Obj>- Flips keys with values of an object typeObj.DeepImmutable<Obj>- Recursively turns the proprties within a given object typeTimmutable, as in have all the properties with thereadonlymodifier.Deepmutable<Obj>- Recursively mutates all the proprties within a given object typeT, as in have all the properties without thereadonlymodifier.DeepRequired<Obj>- Recursively make all object properties required.DeepNotRequired<Obj>- Recursively make all object properties not required.DeepOmit<Obj,P>- Recursively omits specified nested properties from an object, based on a given predicateP.DeepPick<Obj,P>- Deeply pick properties from a nested object, based on a given predicateP.EmptyObject- Represents any non-nullish value, basically{}.EqualStrlen<S1, S2>- Check if two stringsS1andS2have the same length.Strlen<S>- Computes the length of a stringSat the type level.StrMin<S, Min>- Enforces that a stringShas a length greater than or equal toMin, returning aMessageon violation.StrMax<S, Max>- Enforces that a stringShas a length less than or equal toMax, returning aMessageon violation.StrBetween<S, Min, Max>- Ensures a stringShas a length within[Min, Max], inclusive, returning aMessageif outside the range.PartialExcept<T, P>- Makes all properties inToptional except those inKwhich remain required.FilterBy<Obj, P>- Filters keys from the object typeObjbased on a specified predicateP.Float<N>- Type representing a float.If<C, Do, Else>- IfCevaluatestrue,Do, otherwise returnElse.IfEquals<T, P, Do, Else>- Checks if typeTis equal to typeP. IfTis equal toP, the type resolves toDo, otherwiseElse.IfExtends<T, P, Do, Else>- Checks if typeTextends typeP. if it does, the type resolves toDo, otherwiseElse.ImmutableKeys<Obj>- Retrieves the keys that are immutable (readonly) from an object of typeObj.Integer<N>- Represents an integer.Abs<N>- Get the absolute value of aNumeric.And<B1,B2>- Logical AND between two boolean types.EitherOneOrMany<T>- Represents a type that can be either a single value of typeTor an array of values of typeT.Nullable- Represents any non-nullish value, basically{}.Equals<X,Y>- Checks if two typesXandYare exactly equal.EvenNumeric<T>- Represents an evenNumeric.ExcludeNull<T>- Excludesnullfrom a typeT.ExcludeNullable<T>- ExcludesNullablefrom a typeT.ExcludeUndefined<T>- Excludesundefinedfrom a typeT.KeysOfUnion<T>- Extracts the union of keys from a given union of object types, useful for accessing all possible keys in unions.Simplify<T>- Flattens the structure of a type by resolving intersections and simplifying nested mapped types, enhancing readability.Extends<T,U>- Evaluates whether one typeTis assignable to another typeU.Falsy- Represents a type that is falsy JavaScript.FalsyProperties<T>- Extracts falsy properties from an object typeT.IsArrayIncludesTypeof<Arr, T>- Checks if an array typeArrincludes one or more ofTtype.LastElement<Arr, T>- Get the last element of the array.IsBigInt<T>- Checks ifTis abigint.IsBoolean<T>- Checks ifTis aboolean.IsDeepImmutable<Obj>- Checks if all the nested properties of a given objectObjare immutable.IsDeepMutable<Obj>- Checks if all the nested properties of a given objectObjare mutable.IsDeepNotRequired<Obj>- Checks if all the properties of a given object (nested) are not required, as in, all properties have the?modifier.IsDeepRequired<Obj>- Checks if all the properties of a given object (nested) are required, as in, all properties do not have the?modifier.IsExactlyAny<T>- Checks if a typeTis exactlyany.IsExactlyBigInt<T>- Checks if a typeTis exactlybigintnot a subtype of it.IsExactlyNumber<T>- Checks if a typeTis exactlynumbernot a subtype of it.IsExactlyString<T>- Checks if a typeTis exactlystringnot a subtype of it.IsExactlySymbol<T>- Checks if a typeTis exactlysymbolnot a subtype of it.IsExactlyUnknown<T>- Checks if a typeTis exactlyunknownnot a subtype of it.IsFalsy<T>- Checks if a given typeTisFalsy.IsFloat<N>- Checks if a given typeTis aFloat<N>.IsFunction<T>- Checks if a given typeTis a function.IsInteger<N>- Checks if a givenNumericis anInteger<N>.IsNever<T>:- Checks if a typeTdoes not resolve, sonever.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 given typeTqualifies as an object.IsString<T>- Check if a given typeTis astring.IsSymbol<T>- Check if a given typeTis asymbol.IsTruthy<T>- Check if a given typeTresolves to a truthy value.Keys<T>- Retrieves the union type of keys (property names) of a typeT.Maybe<T>- Type that might beNullableMaybeUndefined<T>- Type that mightundefined.MutableKeys<Obj>- Retrieves the keys that are mutable from an object of typeObj.Nand<B1, B2>- LogicalNANDbetween two boolean typesB1andB2.NegativeFloat<N>- Represents a negative (]-∞, 0[)Float<N>.NegativeFloatString<S>- Represents a negativeFloat<N>parsed from astring.NegativeInteger<N>- Represents a negative (]-∞, 0[)Integer<N>.NegativeIntegerString<S>- Represents a negativeInteger<N>parsed from astring.NewType<New, Base>- Represents a new unique type derived from an existing base type. (branded type)Newable- Represents constructor functions that can be invoked using the new keyword.NonRequiredKeys<Obj>- Returns all non required keys of an objectObj, as in any property of an object that is marked with?operator.Not<B>- Negates a boolean typeB.Nullable- Represents a type that can either benullorundefined.Numeric- Represents a type that can either benumberorbigint.NumerifyString<S>- Turn a given string literal to aNumeric, if possible.Methods<Obj>- Get the literal names of keys that are methods in an object typeObj.Properties<Obj>- Get the literal names of keys that are properties in an object typeObj.OddNumeric<T>- Represents an oddNumeric.OmitByType<Obj, T>- Get a set of properties fromObjwhose type are not assignable toT.OmitCommonKeys<Obj1, Obj2>- Omit any common key between the the two objects,.OmitExactlyByType<Obj, T>- Omit properties fromObjwhose type exactly matchesT.Optional<T>- Represents a type that may benull, similar to Python'sOptionaltype and Rust'sOptionenum.NotAssignableTo<U, V>- Represents a type that excludes all members ofUthat are assignable toV.Or<B1, B2>- LogicalORbetween two boolean typesB1andB2.PickByType<Obj, T>- Pick fromObja set of properties that match the typeT.PickCommonKeys<Obj1, Obj2>- Get the common keys between two objects.PickExactlyByType<Obj, T>- Get a set of properties fromObjwhose type exactly matchesT.PositiveFloat<N>- Represents a positive ([0, +∞[)Float<N>.PositiveFloatString<S>- Represents a positiveFloat<N>parsed from astring.PositiveInteger<N>- Represents a positive ([0, +∞[)Integer<N>.PositiveIntegerString<S>- Represents a negativeInteger<N>parsed from astring.Primitive- All primitive types.ReplaceKeys<Obj1,P,Obj2>- Constructs a new type by replacing propertiesPof typeObjwith object typeObj2.RequiredKeys<Obj>- Get the required keys of an object (shallow).SizedTuple<T,N>- Creates a tuple with a specific length, where each element is of a given type.StringEndsWith<S,E>- Checks if a stringSends withEStringStartsWith<S,St>- Checks if a stringSstarts withStStringifyPrimitive<P>- Turns a givenPrimitivevalue (exceptsymbol) into its string representation.Strlen<S>- Get the length of a stringS.TestType<T1, T2, Expected>- Tests if typeT1andT2are the same.TruthyProperties<T>- Extracts truthy properties from an object typeT.UnionToIntersection<U>- As the name implies, it turns a union into an intersection.Vals<Obj>- Get the set of type values in a given object.Xor<B1, B2>- ExclusiveORbetween two boolean typesB1andB2.Xnor<B1, B2>- ExclusiveXNORbetween two boolean typesB1andB2.
The best way to understand how these types work is to check the tests directory. Each type has corresponding ~~edge~~ test cases that demonstrate its usage and expected behavior.
Changelog
See releases.
