{"id":19408605,"url":"https://github.com/morglod/tsargs","last_synced_at":"2025-07-31T05:07:46.761Z","repository":{"id":57381399,"uuid":"147102679","full_name":"Morglod/tsargs","owner":"Morglod","description":"TypeScript utility types for function arguments","archived":false,"fork":false,"pushed_at":"2020-11-04T19:54:43.000Z","size":30,"stargazers_count":28,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-20T06:04:48.708Z","etag":null,"topics":["arguments","functions","types","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morglod.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-02T16:31:06.000Z","updated_at":"2023-07-06T22:22:11.000Z","dependencies_parsed_at":"2022-09-05T13:40:15.853Z","dependency_job_id":null,"html_url":"https://github.com/Morglod/tsargs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Morglod/tsargs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morglod%2Ftsargs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morglod%2Ftsargs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morglod%2Ftsargs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morglod%2Ftsargs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morglod","download_url":"https://codeload.github.com/Morglod/tsargs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morglod%2Ftsargs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267988945,"owners_count":24177016,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arguments","functions","types","typescript"],"created_at":"2024-11-10T12:06:44.910Z","updated_at":"2025-07-31T05:07:46.736Z","avatar_url":"https://github.com/Morglod.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM Version](https://badge.fury.io/js/tsargs.svg?style=flat)](https://www.npmjs.com/package/tsargs) [![NPM Downloads](https://img.shields.io/npm/dw/tsargs)](https://www.npmjs.com/package/tsargs)\n\n# tsargs\n\nTypeScript utility types for function arguments  \nUse [Parameters\\\u003cT\\\u003e](https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype) instead, if you dont need something special\n\n* [Pick type of argument](#pick-argument)\n* [Pick args of class constructor](#class-constructor)\n* [Get arguments number](#get-arguments-number)\n* [Replace return type of function](#replace-return-type-of-function)\n* [Pick specific range of arguments](#pick-range-of-arguments)\n* [Prepend arguments](#prepend-arguments)\n* [Append arguments](#append-arguments)\n\nCheckout [typed event emitter](https://github.com/Morglod/tsee) for real-world example.\n\n[Tested with static asserts](/lib/test.ts)\n\n⚠️ Should use latest typescript version. ⚠️\n\nFor typescript `2.8.x` switch to `ts280` branch.\n\n\u003cdetails\u003e\n\u003csummary\u003e\nHow to use ts280 branch\n\u003c/summary\u003e\n\n```\nnpm install git://github.com/morglod/tsargs.git#ts280\n```\n\n\u003c/details\u003e\n\n## Install \u0026 use\n\n```\nnpm i tsargs\n```\n\n```ts\nimport { ArgI, Arg2, Args } from 'tsargs';\n\nfunction foo(a: number, b: string) {}\n\n// Pick all args\nconst args: Args\u003ctypeof foo\u003e = [ 123, 'hello' ];\n\n// Pick by index\nconst firstArg: ArgI\u003ctypeof foo, 0\u003e = 'hello world!';\n\n// Pick by definition\nconst secondArg: Arg2\u003ctypeof foo\u003e = 'hello world!';\n```\n\n## Pick argument\n\nUse `ArgI\u003cFunction, Index\u003e` to pick argument by it's index.\n\n```ts\nimport { ArgI } from 'tsargs';\n\nfunction foo(a: number, b: string) {}\n\nconst secondArg: ArgI\u003ctypeof foo, 1\u003e = 'hello world!';\n```\n\n\u003cdetails\u003e\n\u003csummary\u003e\nArgN variant\n\u003c/summary\u003e\n\nUse Arg`N` type to pick `N` argument (max 10 arg)  \n\n```ts\nimport { Arg2 } from 'tsargs';\n\nfunction foo(a: number, b: string) {}\n\nconst secondArg: Arg2\u003ctypeof foo\u003e = 'hello world!';\n```\n\u003c/details\u003e\n\n## Class constructor\n\nUse `CtorArgs` to pick all args from constructor.\n\n```ts\nimport { CtorArgs } from 'tsargs';\n\nclass A {\n    constructor(\n        public x: number,\n        public y: string,\n        public z: boolean,\n    ) {}\n}\n\nconst args: CtorArgs\u003ctypeof A\u003e = [ 123, 'hello', false ];\n```\n\n## Get arguments number\n\n```ts\nimport { ArgsNum } from 'tsargs';\n\nfunction foo(a: number, b: string): number { return 0; }\nfunction boo(a: number, b: string, c: boolean): string { return '0'; }\n\nconst fooI: ArgsNum\u003ctypeof foo\u003e = 2;\nconst booI: ArgsNum\u003ctypeof boo\u003e = 3;\n```\n\n## Replace return type of function\n\n```ts\nimport { ReplaceReturn } from 'tsargs';\n\nfunction foo(a: number, b: string): number {}\nfunction boo(a: number, b: string): string {}\n\nconst booFromFoo: ReplaceReturn\u003cstring, typeof foo\u003e = boo;\n```\n\n## Prepend arguments\n\nUseful for typed callbacks, rpc or event \n\nUse Pre`N`Arg`J` type to prepend `N` arguments to function with `J` arguments (max 10 arg)  \nor Pre`N`ArgN type to prepend `N` arguments to function with unknown arguments number\n\n```ts\nimport { Pre1Arg2, Pre1ArgN } from 'tsargs';\n\nfunction foo(a: number, b: string) {}\nfunction boo(x: string, a: number, b: string) {}\n\nconst booFromFoo: Pre1Arg2\u003cstring, typeof foo\u003e = boo;\nconst booFromFoo2: Pre1ArgN\u003cstring, typeof foo\u003e = boo;\n```\n\nPre`N`ArgN type may cause low ts performance\n\n## Append arguments\n\nUseful for typed callbacks, rpc or event \n\nUse Post`N`Arg`J` type to append `N` arguments to function with `J` arguments (max 10 arg)  \nor Post`N`ArgN type to append `N` arguments to function with unknown arguments number\n\n```ts\nimport { Post1Arg2, Post1ArgN } from 'tsargs';\n\nfunction foo(a: number, b: string) {}\nfunction boo(a: number, b: string, x: string) {}\n\nconst booFromFoo: Post1Arg2\u003cstring, typeof foo\u003e = boo;\nconst booFromFoo2: Post1ArgN\u003cstring, typeof foo\u003e = boo;\n```\n\nPost`N`ArgN type may cause low ts performance\n\n## Pick range of arguments\n\nCallbacks \u0026 arguments list\n\nUse `Args10` to pick 10 args of function  \n\nUse Args`N`off`Offset` to pick `N` args with `Offset` offset (max 10 arg)\n\n```ts\nimport { Args2off1 } from 'tsargs';\n\nfunction foo(a: boolean, b: number, c: string) {}\nconst argsBC: Args2off1\u003ctypeof foo\u003e = [ 123, 'Hello' ];\n```\n\nUse `Args` to pick all arguments\n\n```ts\nimport { Args } from 'tsargs';\n\nfunction foo(a: boolean, b: number, c: string) {}\nconst argsABC: Args\u003ctypeof foo\u003e = [ true, 123, 'Hello' ];\n```\n\n`Args` could be used in rest arguments:\n\n```ts\nconst myCallbacks = {\n    foo: (a: number, b: number) =\u003e a + b,\n    boo: (a: number) =\u003e a + 10,\n};\n\nfunction call\u003c\n    CallbackName extends keyof Callbacks,\n    Callbacks extends { [k: string]: (...args: any[]) =\u003e any } = typeof myCallbacks,\n    Callback extends (...args: any[]) =\u003e any = Callbacks[CallbackName],\n\u003e(\n    callbackName: CallbackName,\n    ...args: Args\u003cCallback\u003e // \u003c\u003c\u003c\u003c---------------\n): ReturnType\u003cCallback\u003e {\n    return (myCallbacks as { [k: string]: Function })[callbackName as any](...args);\n}\n\ncall('foo', 1, 2); // ok\ncall('boo', 1, 2); // Error: Should be 2 args, recieved 3\n```\n\nCheckout [typed event emitter](https://github.com/Morglod/tsee) for real-world example.\n\n## Roadmap\n\n* ✔️ [Example of typed event emitter](https://github.com/Morglod/tsee)\n* ✔️ Pick range of arguments to array type\n* ✔️ Pick any number of arguments to array type\n* ❌ Pick arguments to object (not needed, use tuples instead)\n* ✔️ Replace return type\n* Specific argument's type replace\n* Remove arguments\n\n[Write issue on github](https://github.com/Morglod/tsargs/issues) if you have any trouble with arguments in typescript\n\n## Contributors\n\nThanks [CallumDenby](https://github.com/CallumDenby) for ArgI solution!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorglod%2Ftsargs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorglod%2Ftsargs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorglod%2Ftsargs/lists"}