A collection of types which can help in the future project
npm install -D ts-type-helpers
Fix unchecked indexes error in TS. Converts interface
into object
.
import { ToObject } from 'ts-type-helpers';
interface Int {
a: string;
b: number;
}
const fn = <T extends Record<string, unknown>>(arg: T) => arg;
fn<Int>; // Unchecked indexes error
fn<ToObject<Int>>; // Withot error
Extract an array union type
import { ArrayType } from 'ts-type-helpers';
ArrayType<Array<'a' | 'b' | 'c'>>; // 'a' | 'b' | 'c'
ArrayType<'a' | 'b' | 'c'>; // 'a' | 'b' | 'c'
Lists digits from zero to a set digit
import { Enumerated } from 'ts-type-helpers';
Enumerated<5>; // 0 | 1 | 2 | 3 | 4
Compares to types they are the same type
Returns TPositive
value or TNegative
value
import { IsEqual } from 'ts-type-helpers';
IsEqual<{ str: string }, { num: number }, 1, 0>; // 0
IsEqual<{ str: string }, { str: string }, 1, 0>; // 1
Just a Record of types with the key type
import { EntitiesMap } from 'ts-type-helpers';
EntitiesMap<{ id: number; value: string }, number>; // Record<number, { id: number; value: string } | undefined>
Type for normalized list with a manual key type
import { NormalizedList } from 'ts-type-helpers';
NormalizedList<{ id: number; value: string }, number>; // { ids: number[]; entities: Record<number, { id: number; value: string } | undefined> }
NormalizedList<{ id: number; value: string }, string>; // { ids: string[]; entities: Record<string, { id: number; value: string } | undefined> }
Type for normalized list which extract key type by its name in object
import { NormalizedListByKey } from 'ts-type-helpers';
NormalizedListByKey<{ id: number; value: string }, 'id'>; // { ids: number[]; entities: Record<number, { id: number; value: string } | undefined> }
NormalizedListByKey<{ id: number; value: string }, 'value'>; // { ids: string[]; entities: Record<string, { id: number; value: string } | undefined> }
Pick object by parameters type
import { PickByType } from 'ts-type-helpers';
PickByType<{ foo: string; bar: number }, string>; // { foo: string }
PickByType<{ foo: string; bar: number }, number>; // { bar: number }
Pick only readonly parameters
import { PickReadOnly } from 'ts-type-helpers';
PickReadOnly<{ readonly a: string; b: string }>; // { readonly a: string; }
Returns keys of only readonly parameters
import { PickReadOnlyKeys } from 'ts-type-helpers';
PickReadOnlyKeys<{ readonly a: string; b: string }>; // 'a'
Pick parameters without readonly
import { PickReadOnly } from 'ts-type-helpers';
PickReadOnly<{ readonly a: string; b: string }>; // { b: string; }
Returns keys of parameters without readonly
import { PickWritableKeys } from 'ts-type-helpers';
PickWritableKeys<{ readonly a: string; b: string }>; // 'b'
Returns a range from TMin to TMax (without TMax)
import { Range } from 'ts-type-helpers';
Range<1, 5>; // 1 | 2 | 3 | 4
Converts object keys from snake_case into camelCase
import { ToCamelCaseDict } from 'ts-type-helpers';
ToCamelCaseDict<{ readonly some_snake_case_str: string }>; // { readonly someSnakeCaseStr: string }
Converts snake_case string into camelCase
import { ToCamelCaseStr } from 'ts-type-helpers';
ToCamelCaseStr<'some_snake_case_str'>; // 'someSnakeCaseStr'
Converts unions to several lists of this unions Helps when you need enumerate unions in a list
import { UnionsToTuple } from 'ts-type-helpers';
UnionsToTuple<'a' | 'b'>; // ['a', 'b'] | ['b', 'a']