Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
TypeScript utility types for function arguments
Use Parameters<T> instead, if you dont need something special
Checkout typed event emitter for real-world example.
⚠️ Should use latest typescript version. ⚠️
For typescript 2.8.x
switch to ts280
branch.
npm install git://github.com/morglod/tsargs.git#ts280
npm i tsargs
import { ArgI, Arg2, Args } from 'tsargs';
function foo(a: number, b: string) {}
// Pick all args
const args: Args<typeof foo> = [ 123, 'hello' ];
// Pick by index
const firstArg: ArgI<typeof foo, 0> = 'hello world!';
// Pick by definition
const secondArg: Arg2<typeof foo> = 'hello world!';
Use ArgI<Function, Index>
to pick argument by it's index.
import { ArgI } from 'tsargs';
function foo(a: number, b: string) {}
const secondArg: ArgI<typeof foo, 1> = 'hello world!';
Use ArgN
type to pick N
argument (max 10 arg)
import { Arg2 } from 'tsargs';
function foo(a: number, b: string) {}
const secondArg: Arg2<typeof foo> = 'hello world!';
Use CtorArgs
to pick all args from constructor.
import { CtorArgs } from 'tsargs';
class A {
constructor(
public x: number,
public y: string,
public z: boolean,
) {}
}
const args: CtorArgs<typeof A> = [ 123, 'hello', false ];
import { ArgsNum } from 'tsargs';
function foo(a: number, b: string): number { return 0; }
function boo(a: number, b: string, c: boolean): string { return '0'; }
const fooI: ArgsNum<typeof foo> = 2;
const booI: ArgsNum<typeof boo> = 3;
import { ReplaceReturn } from 'tsargs';
function foo(a: number, b: string): number {}
function boo(a: number, b: string): string {}
const booFromFoo: ReplaceReturn<string, typeof foo> = boo;
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
import { Pre1Arg2, Pre1ArgN } from 'tsargs';
function foo(a: number, b: string) {}
function boo(x: string, a: number, b: string) {}
const booFromFoo: Pre1Arg2<string, typeof foo> = boo;
const booFromFoo2: Pre1ArgN<string, typeof foo> = boo;
PreN
ArgN type may cause low ts performance
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
import { Post1Arg2, Post1ArgN } from 'tsargs';
function foo(a: number, b: string) {}
function boo(a: number, b: string, x: string) {}
const booFromFoo: Post1Arg2<string, typeof foo> = boo;
const booFromFoo2: Post1ArgN<string, typeof foo> = boo;
PostN
ArgN type may cause low ts performance
Callbacks & arguments list
Use Args10
to pick 10 args of function
Use ArgsN
offOffset
to pick N
args with Offset
offset (max 10 arg)
import { Args2off1 } from 'tsargs';
function foo(a: boolean, b: number, c: string) {}
const argsBC: Args2off1<typeof foo> = [ 123, 'Hello' ];
Use Args
to pick all arguments
import { Args } from 'tsargs';
function foo(a: boolean, b: number, c: string) {}
const argsABC: Args<typeof foo> = [ true, 123, 'Hello' ];
Args
could be used in rest arguments:
const myCallbacks = {
foo: (a: number, b: number) => a + b,
boo: (a: number) => a + 10,
};
function call<
CallbackName extends keyof Callbacks,
Callbacks extends { [k: string]: (...args: any[]) => any } = typeof myCallbacks,
Callback extends (...args: any[]) => any = Callbacks[CallbackName],
>(
callbackName: CallbackName,
...args: Args<Callback> // <<<<---------------
): ReturnType<Callback> {
return (myCallbacks as { [k: string]: Function })[callbackName as any](...args);
}
call('foo', 1, 2); // ok
call('boo', 1, 2); // Error: Should be 2 args, recieved 3
Checkout typed event emitter for real-world example.
Write issue on github if you have any trouble with arguments in typescript
Thanks CallumDenby for ArgI solution!
FAQs
typescript function arguments utils
The npm package tsargs receives a total of 3,064 weekly downloads. As such, tsargs popularity was classified as popular.
We found that tsargs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.