Advanced utility types for TypeScript
# NPM
npm install ts-pro
# YARN
yarn add ts-pro
List — tuple or any array. All types are applied first to tuples, and in the case of a mismatch, to the rest of the common array cases.
[0] - [ ] - [ ] - [ ] - ...
Head(List) ^ never
first element of list
P_List.Head<string[]>; // string | undefined (undefined for empty array)
P_List.Head<['a', 1, null]>; // 'a'
P_List.Head<[]>; // never
[ ] - [0] - [0] - [0] - ...
Tail(List) ^ never
all elements of list after first element
P_List.Tail<string[]>; // (string | undefined)[] (undefined for empty array and array from one item)
P_List.Tail<['a', 1, null]>; // [1, null]
P_List.Tail<[]>; // never
[A] - [A] - [A] - ... => A
[A] - [B] - [C] - ... => (A | B | C | ...)
Item(List) ^ never
type of any element in list
P_List.Item<string[]>; // string
P_List.Item<['a', 1, null]>; // 'a' | 1 | null
P_List.Item<[]>; // never
Filtered list
P_List.Filter<[true, false[], boolean], boolean>; // [true, boolean]
P_List.Filter<[1, 2, 2 | 3, 9, 1 | 9], 2 | 3 | 9>; // [2, 2 | 3, 9]
P_List.Filter<string[], unknown>; // string[]
P_List.Filter<number[], boolean>; // []
Mapped list with T
filling
P_List.Map<[1, 2, 3], 0>; // [0, 0, 0]
P_List.Map<string[], 0>; // 0[]
Filtered list with fill-in never
in place of mismatches
P_List.FilterMap<[1, number, 'a', string], string>; // [never, never, 'a', string]
P_List.FilterMap<boolean[], string>; // never[]
subtype(T) ^ never
found subtype
P_List.Find<[true, boolean, 2, number, 3], number>; // 2
P_List.Find<number[], string>; // never
true ^ false
— every item of list is subtype T
P_List.Every<[1, 9.4, typeof Infinity, number], number>; // true
P_List.Every<number[], string>; // false
Reverted list
P_List.Revert<[1, 'a', string]>; // [string, 'a', 1]
P_List.Revert<number[]>; // number[]
Removed W
from T
P_String.Filter<'a b c', ' '>; // 'abc'
P_String.Filter<'123', '2'>; // '13'
Removed W
from T
in start and end
P_String.Trim<' 897 '>; // '897'
P_String.Trim<'__get', '_'>; // 'get'
Reverse string
P_String.Reverse<'1,2,3,4,5'>; // '5,4,3,2,1'
Replace W
to R
in T
P_String.Replace<'Hello. Go away.', '.', '!'>; // 'Hello! Go away!'
P_String.Replace<P_String.Replace<
'#{count} points in category #{category}',
'#{count}', '12'>,
'#{category}', 'typescript'>;
// '12 points in category typescript'
FilterByValue<{a: 1; b: 'one'; c: number}, number>; // {a: 1; c: number}
FilterByKey<{a: 1; b: 'one'}, 'b' | 'c'>; // {b: 'one'}
any function (...args: any) => any
Filtered list of function parameters
P_Function.FilterParameters<(a: string, b: number) => string, string>; // [string, never]
Result function for chaining functions
f: A => B
|> g: B => C
|> h: C => D
~ x => h(g(f(x)))
~ A => D
P_Function.Pipe<[(a: string) => number, (a: number) => boolean]>; // (a: string) => boolean
P_Function.Pipe<[(a: string) => number, (a: string) => boolean]>; // never (number not extends string)
P_Function.Pipe<((a: 'a' | 'b' | 'c') => 'a' | 'b')[]>; // (a: 'a' | 'b' | 'c') => 'a' | 'b'
P_Function.Pipe<((a: 'a' | 'b') => 'a' | 'b' | 'c')[]>; // never
Result function for compose functions
h: C => D
<| g: B => C
<| f: A => B
~ x => h(g(f(x)))
~ A => D
P_Function.Compose<[(a: string) => boolean, (a: number) => string]>; // (a: number) => boolean
P_Function.Compose<[(a: string) => number, (a: string) => boolean]>; // never (bollean not extends string)
P_Function.Compose<((a: 'a' | 'b' | 'c') => 'a' | 'b')[]>; // (a: 'a' | 'b' | 'c') => 'a' | 'b'
P_Function.Compose<((a: 'a' | 'b') => 'a' | 'b' | 'c')[]>; // never
Tuple — fixed-length array.
Empty tuple []
Non-empty tuple [T, ...T[]]
Tuple Empty | NonEmpty<T>
Head
, Tail
, Item
, Filter
, Map
, FilterMap
, Find
, Every
, Revert
Array T[]
Head
, Tail
, Item
, Filter
, Map
, FilterMap
, Find
, Every
First non never type in list or never
.
Selecting types in turn.
P_Options<[never, never, string, number]>; // string
// in generics
type stringName<T> = T extends string ? 'string' : never;
type numberName<T> = T extends number ? 'number' : never;
type booleanName<T> = T extends boolean ? 'boolean' : never;
type typeName<T> = P_Options<[stringName<T>, numberName<T>, booleanName<T>, 'unknown']>;
typeName<1>; // 'number'
typeName<string[]>; // 'unknown'
T | undefined