All Projects → Morglod → Tsargs

Morglod / Tsargs

TypeScript utility types for function arguments

Programming Languages

typescript
32286 projects
types
53 projects

Labels

Projects that are alternatives of or similar to Tsargs

forlab
Forlab is a Fortran module that provides a lot of functions for scientific computing mostly inspired by Matlab and Python's package NumPy.
Stars: ✭ 26 (+13.04%)
Mutual labels:  functions
Microcule
SDK and CLI for spawning streaming stateless HTTP microservices in multiple programming languages
Stars: ✭ 454 (+1873.91%)
Mutual labels:  functions
Actor
A type-safe Actor class and WiredActor for sending functions as messages
Stars: ✭ 16 (-30.43%)
Mutual labels:  functions
dotfiles
My dotfiles
Stars: ✭ 16 (-30.43%)
Mutual labels:  functions
Faas
OpenFaaS - Serverless Functions Made Simple
Stars: ✭ 20,820 (+90421.74%)
Mutual labels:  functions
Reattempt
🤞 Give your functions another chance
Stars: ✭ 570 (+2378.26%)
Mutual labels:  functions
js-training
JS Training Course
Stars: ✭ 39 (+69.57%)
Mutual labels:  functions
Functions.js
📦 A hub of numerous functions with various functionalities
Stars: ✭ 22 (-4.35%)
Mutual labels:  functions
Nuclio
High-Performance Serverless event and data processing platform
Stars: ✭ 4,213 (+18217.39%)
Mutual labels:  functions
Fission
Fast and Simple Serverless Functions for Kubernetes
Stars: ✭ 6,646 (+28795.65%)
Mutual labels:  functions
js-utils
A collection of dependency-free JavaScript utilities 🔧
Stars: ✭ 22 (-4.35%)
Mutual labels:  functions
Pandasvault
Advanced Pandas Vault — Utilities, Functions and Snippets (by @firmai).
Stars: ✭ 316 (+1273.91%)
Mutual labels:  functions
Hibernate Springboot
Collection of best practices for Java persistence performance in Spring Boot applications
Stars: ✭ 589 (+2460.87%)
Mutual labels:  functions
invokable
Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs).
Stars: ✭ 40 (+73.91%)
Mutual labels:  functions
Immutable
Missing non-mutating functions in Swift
Stars: ✭ 16 (-30.43%)
Mutual labels:  functions
VBA-Arrays
😎 Array functions that are similar JavaScript functions. Example: Push, Pop, Shift, Unshift, Sort, length, toString.
Stars: ✭ 48 (+108.7%)
Mutual labels:  functions
Dispatch
Dispatch is a framework for deploying and managing serverless style applications.
Stars: ✭ 529 (+2200%)
Mutual labels:  functions
Introduction To Programming With Matlab
Coursera Course: Introduction to Programming 👩‍💻 with MATLAB ~by Vanderbilt University 🎓
Stars: ✭ 23 (+0%)
Mutual labels:  functions
Threef.js
three.js addon, to produce almost infinite many time-varying geometries / buffer geometries with functions (cylindrical coordinates)
Stars: ✭ 19 (-17.39%)
Mutual labels:  functions
Openwhisk
Apache OpenWhisk is an open source serverless cloud platform
Stars: ✭ 5,499 (+23808.7%)
Mutual labels:  functions

NPM Version NPM Downloads

tsargs

TypeScript utility types for function arguments
Use Parameters<T> instead, if you dont need something special

Checkout typed event emitter for real-world example.

Tested with static asserts

⚠️ 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
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!';

Pick argument

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!';
ArgN variant

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!';

Class constructor

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 ];

Get arguments number

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;

Replace return type of function

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;

Prepend arguments

Useful for typed callbacks, rpc or event

Use PreNArgJ type to prepend N arguments to function with J arguments (max 10 arg)
or PreNArgN 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;

PreNArgN type may cause low ts performance

Append arguments

Useful for typed callbacks, rpc or event

Use PostNArgJ type to append N arguments to function with J arguments (max 10 arg)
or PostNArgN 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;

PostNArgN type may cause low ts performance

Pick range of arguments

Callbacks & arguments list

Use Args10 to pick 10 args of function

Use ArgsNoffOffset 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.

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].