From 77baa4527d04d242551d76d0ad63a938c42e9487 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Sat, 29 May 2021 17:18:17 +0900 Subject: [PATCH 1/3] feat: add generics to OptionValues --- typings/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index ee40d8990..1513383c1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -156,9 +156,9 @@ declare namespace commander { type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll'; - interface OptionValues { - [key: string]: any; - } + type OptionValues = { + [key in keyof T]: T[key] + }; interface Command { args: string[]; @@ -384,8 +384,8 @@ declare namespace commander { * * @returns `this` command for chaining */ - storeOptionsAsProperties(): this & OptionValues; - storeOptionsAsProperties(storeAsProperties: true): this & OptionValues; + storeOptionsAsProperties(): this & OptionValues; + storeOptionsAsProperties(storeAsProperties: true): this & OptionValues; storeOptionsAsProperties(storeAsProperties?: boolean): this; /** @@ -485,7 +485,7 @@ declare namespace commander { /** * Return an object containing options as key-value pairs */ - opts(): OptionValues; + opts(): OptionValues; /** * Set the description. From 4d6bfa39436260b7acce3e976b4711ea682801ad Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Sun, 30 May 2021 10:10:49 +0900 Subject: [PATCH 2/3] feat: use extends --- typings/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 1513383c1..78938ef08 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -156,9 +156,9 @@ declare namespace commander { type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll'; - type OptionValues = { - [key in keyof T]: T[key] - }; + interface OptionValues { + [key: string]: any; + } interface Command { args: string[]; @@ -384,8 +384,8 @@ declare namespace commander { * * @returns `this` command for chaining */ - storeOptionsAsProperties(): this & OptionValues; - storeOptionsAsProperties(storeAsProperties: true): this & OptionValues; + storeOptionsAsProperties(): this & T; + storeOptionsAsProperties(storeAsProperties: true): this & T; storeOptionsAsProperties(storeAsProperties?: boolean): this; /** @@ -485,7 +485,7 @@ declare namespace commander { /** * Return an object containing options as key-value pairs */ - opts(): OptionValues; + opts(): T; /** * Set the description. From 5ff80a37d7b896734c3d97a21be2e53a72ca6f99 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Sun, 30 May 2021 11:50:07 +0900 Subject: [PATCH 3/3] test: add opts case --- typings/index.test-d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index ca6ee0a7f..af39812de 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -171,6 +171,15 @@ expectType(opts); expectType(opts.foo); expectType(opts['bar']); +// opts with generics +interface MyCheeseOption { + cheese: string; +} +const myCheeseOption = program.opts(); +expectType(myCheeseOption.cheese); +// @ts-expect-error Check that options strongly typed and does not allow arbitrary properties +expectType(myCheeseOption.foo); + // description expectType(program.description('my description')); expectType(program.description());