From 205419a1313f2549f2490b8a8b7dbb746b61866f Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Fri, 18 Jun 2021 10:28:02 -0700 Subject: [PATCH 1/2] feat: extend `Base` constructor options with plugins --- index.test-d.ts | 6 ++++++ plugins/with-options/index.d.ts | 10 ++++++++++ plugins/with-options/index.js | 11 +++++++++++ 3 files changed, 27 insertions(+) create mode 100644 plugins/with-options/index.d.ts create mode 100644 plugins/with-options/index.js diff --git a/index.test-d.ts b/index.test-d.ts index f8fcddc..dc522d0 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -4,6 +4,7 @@ import { Base } from "./index.js"; import { fooPlugin } from "./plugins/foo/index.js"; import { barPlugin } from "./plugins/bar/index.js"; import { voidPlugin } from "./plugins/void/index.js"; +import { withOptionsPlugin } from "./plugins/with-options"; const base = new Base(); @@ -48,3 +49,8 @@ expectType(baseWithVoidAndNonVoidPlugins.bar); // @ts-expect-error unknown properties cannot be used, see #31 baseWithVoidAndNonVoidPlugins.unknown; + +const BaseWithOptionsPlugin = Base.plugin(withOptionsPlugin); +const baseWithOptionsPlugin = new BaseWithOptionsPlugin(); + +expectType(baseWithOptionsPlugin.getFooOption()); diff --git a/plugins/with-options/index.d.ts b/plugins/with-options/index.d.ts new file mode 100644 index 0000000..4214b68 --- /dev/null +++ b/plugins/with-options/index.d.ts @@ -0,0 +1,10 @@ +import { Base } from "../.."; + +// TODO: add "foo" to Base.Options type + +export function withOptionsPlugin( + base: Base, + options: Base.Options +): { + getFooOption: () => Base.Options["foo"]; +}; diff --git a/plugins/with-options/index.js b/plugins/with-options/index.js new file mode 100644 index 0000000..23b84de --- /dev/null +++ b/plugins/with-options/index.js @@ -0,0 +1,11 @@ +/** + * @param {import('../..').Base} base + * @param {import('../..').Base.Options} options + */ +export function withOptionsPlugin(base, options) { + return { + getFooOption() { + return options.foo; + }, + }; +} From cb11cc919f2e837234cfc6938d17873947860db0 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Fri, 18 Jun 2021 13:22:03 -0700 Subject: [PATCH 2/2] feat: extend `Base` constructor options with plugins --- index.d.ts | 6 +++--- plugins/with-options/index.d.ts | 11 +++++++++-- plugins/with-options/index.js | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8a920c8..6489eb8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,7 @@ -export declare namespace Base { - type Options = { +export namespace Base { + interface Options { [key: string]: unknown; - }; + } } declare type ApiExtension = { diff --git a/plugins/with-options/index.d.ts b/plugins/with-options/index.d.ts index 4214b68..7d97f99 100644 --- a/plugins/with-options/index.d.ts +++ b/plugins/with-options/index.d.ts @@ -1,10 +1,17 @@ +// https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-plugin-d-ts.html import { Base } from "../.."; -// TODO: add "foo" to Base.Options type +declare module "../.." { + namespace Base { + interface Options { + foo?: string; + } + } +} export function withOptionsPlugin( base: Base, options: Base.Options ): { - getFooOption: () => Base.Options["foo"]; + getFooOption: () => Required["foo"]; }; diff --git a/plugins/with-options/index.js b/plugins/with-options/index.js index 23b84de..7decc7d 100644 --- a/plugins/with-options/index.js +++ b/plugins/with-options/index.js @@ -5,7 +5,7 @@ export function withOptionsPlugin(base, options) { return { getFooOption() { - return options.foo; + return options.foo || "my default"; }, }; }