ts-types-utils
Type utilities for typescript
Table of Contents
Usage
npm i -D ts-type-utils
;
Match
Match<T, M, F> // T is object, M is what to match to, F negate
Pick from T all properties that match M, or not match M if F is false
; ; // Match<T, Function, true>; ; ; // { a: string, b: number }; // { c: () => void; d: (hello: string) => number }
MatchNames
MatchNames<T, M, F> // T is object, M is what to match to, F negate
Get properties names from T that match M, or not match M if F is false
; ; // MatchNames<T, Function, true>; ; ; // "a" | "b"; // "c" | "d"
Assign
Assign<A, B>
like A & B but replaces intersected A types with the ones from B
; ;; ;;
Func
Func<P, R> // P: params type, R: return type
This doesn't really bring much but syntactic sugar
; ; // (a: string, b: number) => boolean
PromiseOr
PromiseOr<T> = T | Promise<T>
Why? Because it gets cumbersome having to repeat long types again and again. I've found myself using this a lot, specially in interfaces (for greater flexibility).
Can be simplified to
Action
Action<T = void> =Action
Why? Because it gets tiring and makes the code less clear having to write function types.
Can be simplified to
UndefinedToOptional
UndefinedToOptional<T>
; foo // Error expected times bar // Good: times is optional now UndefinedToOptional<Options> // { times?: number }
ArgsType ( DEPRECATED: already in std types as Parameters )
ArgsType<F> // F is function
Like built-in ReturnType
but for the args of a function, works for any number of arguments
;; // [string, number]; // string; // number
Related
Tests were made with typescript-test-utils
Contributors
- @danielpa9708
- @cloudrex
- @luisenrike