tsargs
TypeScript utility types for function arguments
Use Parameters<T> instead, if you dont need something special
- Pick type of argument
- Pick args of class constructor
- Get arguments number
- Replace return type of function
- Pick specific range of arguments
- Prepend arguments
- Append arguments
Checkout typed event emitter for real-world example.
⚠️ Should use latest typescript version. ⚠️
For typescript 2.8.x
switch to ts280
branch.
How to use ts280 branch
npm install git://github.com/morglod/tsargs.git#ts280
Install & use
npm i tsargs
; // Pick all args; // Pick by index; // Pick by definition;
Pick argument
Use ArgI<Function, Index>
to pick argument by it's index.
; ;
ArgN variant
Use ArgN
type to pick N
argument (max 10 arg)
; ;
Class constructor
Use CtorArgs
to pick all args from constructor.
; ;
Get arguments number
; ;;
Replace return type of function
; ;
Prepend arguments
Useful for typed callbacks, rpc or event
Use PreN
ArgJ
type to prepend N
arguments to function with J
arguments (max 10 arg)
or PreN
ArgN type to prepend N
arguments to function with unknown arguments number
; ;;
PreN
ArgN type may cause low ts performance
Append arguments
Useful for typed callbacks, rpc or event
Use PostN
ArgJ
type to append N
arguments to function with J
arguments (max 10 arg)
or PostN
ArgN type to append N
arguments to function with unknown arguments number
; ;;
PostN
ArgN type may cause low ts performance
Pick range of arguments
Callbacks & arguments list
Use Args10
to pick 10 args of function
Use ArgsN
offOffset
to pick N
args with Offset
offset (max 10 arg)
; ;
Use Args
to pick all arguments
; ;
Args
could be used in rest arguments:
; CallbackName extends keyof Callbacks, Callbacks extends = typeof myCallbacks, Callback extends ...args: any any = Callbacks,> callbackName: CallbackName, ...args: Args<Callback> // <<<<---------------: ReturnType<Callback> call'foo', 1, 2; // okcall'boo', 1, 2; // Error: Should be 2 args, recieved 3
Checkout typed event emitter for real-world example.
Roadmap
- ✔️ Example of typed event emitter
- ✔️ Pick range of arguments to array type
- ✔️ Pick any number of arguments to array type
- ❌ Pick arguments to object (not needed, use tuples instead)
- ✔️ Replace return type
- Specific argument's type replace
- Remove arguments
Write issue on github if you have any trouble with arguments in typescript
Contributors
Thanks CallumDenby for ArgI solution!