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/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..7d97f99 --- /dev/null +++ b/plugins/with-options/index.d.ts @@ -0,0 +1,17 @@ +// https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-plugin-d-ts.html +import { Base } from "../.."; + +declare module "../.." { + namespace Base { + interface Options { + foo?: string; + } + } +} + +export function withOptionsPlugin( + base: Base, + options: Base.Options +): { + getFooOption: () => Required["foo"]; +}; diff --git a/plugins/with-options/index.js b/plugins/with-options/index.js new file mode 100644 index 0000000..7decc7d --- /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 || "my default"; + }, + }; +}