ts-it-types
TypeScript Useful Types
NPM
npm install ts-it-types
Single file
All types together in ts_it_types.ts
Contents
Map types
Map types are types with key-value structure.
StringMap
StringMap is dictionary type.
; // Only string value enable
ObjectMap
ObjectMap is map with any type values.
; // Any value available
<T>
MapQualifierMapQualifier is builder for custom map type.
Short name of MapQualifier is MapQ
<T> value type
; // Create your own Map Type
Range types
Range types are set of numbers. Using it, you can get free set of numbers.
Type Script 3.4 do not have BuildIn type or capability to declare range numbers type.
It might look like this: type CustomRange = 3..23; for example, but not yet..
Declared Ranges
All range are declared with first letter 'R' and numbers from 1 to 1000 included.
For example, range with 0 to 10 (included) is R10, range from 0 to 50 (included) is R50, and so on.
Declared range with postfix 'n' make exclude last number from range.
For example, range with 0 to 10 (excluded) is R10n, range from 0 to 50 (excluded) is R50n, and so on.
; // [0..23) Hour type; // [0..60) Minute type; // [0..60) Second type; // [0..1000) MilliSecond type;
<From,To>
RangeQualifierRangeQualifier is builder for custom range type.
Qualifier need to declare free range, for example from 17 to 23.
Short name of RangeQualifier is RQ
<From> from range
<To> to range
; // [17 .. 23] Include all numbers from 17 to 23; // [50 .. 100) Include all numbers from 50 to 99. Attention: 100 - excluded;
Buildin Overload
Buildin overload are types with extra features of buildin TypeScript types.
<FromType,KeepType=any,Include=true>
GetNamesGetNames is type to extract properties name, using type filter to include or exclude some names.
<FromType> type to analyze
<KeepType> type to keep. Default is any - all types be included
<Include> way of analyzing. true - include, false - exclude. Default is true.
Extra for keyof T.
; // SC - SimpleClass // be: "one" | "two" | "three" | "getOne" | "getTwo"; // like keyof SimpleClass // be: "one" | "two"; // be: "getOne" | "getTwo"; // be: "three" | "getOne" | "getTwo"; // be: "one" | "two" | "three" | "getOne" | "getTwo" | "prototype" | "ZERO";
<FromType,IgnoreType>
LoseNamesLoseNames is type to extract properties name, using type filter to exclude some names.
LoseNames vice versa of GetNames
<FromType> type to analyze
<IgnoreType> type to ignore
Extra for keyof T.
; ; // be: "name" | "lastName";
<T>
MaybeMaybe is type to define part of code which can contains value or value is absent.
;
TypeSelector`<State extends boolean, FirstType, SecondType>
TypeSelector is type to select one of two types. Its usuful when some interface or type generates 2 linked types.
For example, Server fetch data and Client model data.
<State> true or false state
<FirstType> to be type on true
<SecondType> to be type on false
; /** Human readable alias on true */; /** Human readable alias on false */; /** After fetch data */ /** After PROCESS fetched data */
Statement Control
Statement control is way to control some block of code which can be changed in future
UnreachableStatementError
UnreachableStatementError is error which must be throw in unreachable part of code.
Code is unreachable now but not in the future. So you control changes which will happens.
Short name of UnreachableStatementError is NeverError
UnreachableStatementError declaration
;
Tuple
Tuple utility types
<Tuple extends any[], Addend>
PrependPrepend is a type of pushing a value in the begining of an array
<Tuple> array of data
<Addend> adding value
; ; // be: ["0", "1", "2", "3"];
Reverse`<Tuple extends any[]>
Reverse is a type of reversing an array values
<Tuple> array of data
; ; // be: ["3", "2", "1"];
<T extends string>
TupleOfTupleOf is a type to convert union to array
<T> union of string keys
; // be: "firstName" | "lastName" | "position"; // be: ["firstName", "lastName", "position"];
UnionOf`<Tuple extends any[], Union = never>
UnionOf is a type to convert array to union
<Tuple> array of data
<Union> initial union values. default never
; // be: "firstName" | "lastName" | "position";