diff --git a/README.md b/README.md index 262bb66..a047e8d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ import { arge } from 'arge' Then: -```typescript +```javascript const args = arge(process.argv) ``` @@ -43,6 +43,33 @@ node app.js --dry-run --mode=development --test=false --retries=100 } ``` +## Typescript + +For type safety, `arge` will accept a generic for typing the resulting object. + +### Example + +```typescript +const { foo, bar } = arge(process.argv) +``` + +In the above scenario, `foo` and `bar` will report the following: + +> Property 'foo' does not exist on type 'unknown' + +> Property 'bar' does not exist on type 'unknown' + +To address this, we can apply a generic: + +```typescript +interface Args { + foo: string + bar: number +} + +const { foo, bar } = arge(process.argv) +``` + ## API ### arge(flags, options?) diff --git a/package.json b/package.json index 1a5d18d..0771cf5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "arge", - "version": "1.1.4", + "version": "1.2.0", "description": "A simple utility to parse command line arguments and flags", "keywords": [ "argv", diff --git a/src/index.ts b/src/index.ts index d0b6e55..44aca66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,13 +31,13 @@ const parse = (value: string): Value => { return value } -export const arge = ( +export const arge = ( args: string[], options: Options = { isArgv: true, camelCaseKeys: true, } -): Record => +): T => (options.isArgv !== false ? args.filter((_, index) => index > 1) : args @@ -53,4 +53,4 @@ export const arge = ( ...acc, [`${key.trim()}`]: parse(v.trim()), } - }, {}) + }, {} as T)