1.0.5 • Published 4 years ago
@hjtech/types v1.0.5
@hjtech/types
general
IfEquals<X, Y, A=X, B=never>
Compare fst. and snd. generic type and return thr. or fth.
type R = IfEquals<number, number, number, string>; // number
type R = IfEquals<string, string>; // string
type R = IfEquals<number, string, number, string>; // string
type R = IfEquals<number, string, number>; // neverWritableKeys<T>
Pick the keys of writable member.
type R = WritableKeys<{ readonly foo: 'foo', bar: 'bar', foobar: 'foobar' }>;
type R = "bar" | "foobar"ReadonlyKeys<T>
Pick the keys of readonly member.
type R = ReadonlyKeys<{ readonly foo: 'foo', bar: 'bar', foobar: 'foobar' }>;
type R = "foo"UnionToIntersection<T>
Turn Union To Intersection
type R = UnionToIntersection<A | B>
type R = A & Bfunction
FirstArgument<T>
Return the first parameter
type Result = FirstArgument<(fst: number, sed: string, thr: object) => any>; // number
type Result = numberOmitFirstParamters<T>
Return the rest after first parameter
type Result = OmitFirstParamters<(fst: number, sed: string, thr: object) => any>;
type Result = [sed: string, thr: object]object
ValueOf<T>
Get values type.
type Result = Valueof<{ foo: string, bar: number }>; // string | numberKeysOfType<T, TProp>
Get type matched keys.
type Result = KeysOfType<{ foo: string, bar: number }, string>; // 'foo'FilterOfType<T, TProp>
Get filter members by type.
type Result = FilterOfType<{ foo: string, bar: number }, string>; // { foo: string }RequireAtLeastOne<T, K extends keyof T>
Require at least one member from T
type Target = { foo?: string, bar?: number, other: string }
type TargetLeastOne = RequireAtLeastOne<Target, 'foo' | 'bar'>
// passed
const objA: TargetLeastOne = { foo: 'foo', other: 'other' };
const objB: TargetLeastOne = { bar: 0, other: 'other' };ArrayElement<T>
Get type of element from array
type Result = ArrayElement<[ number, string ]> => number | stringArrayElementMemberType<T, M>
Get type from member in element of array
type TodoList = Array<{ foo: number, bar: string }>
type Result = ArrayElementMemberType<TodoList, 'foo'> // numberpromise
Await<T>
Get type from Promise<T>
type Result = Await<Promise<string>> === stringAwaitReturnType<T>
Get type from call who return the Promise
type Resoult = AwaitReturnType<() => Promise<string>> === string