Skip to content

Commit bd9d789

Browse files
authored
feat: extend Base constructor options with plugins (#56)
Co-Authored-By: [email protected]
1 parent 4c8cec9 commit bd9d789

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export declare namespace Base {
2-
type Options = {
1+
export namespace Base {
2+
interface Options {
33
[key: string]: unknown;
4-
};
4+
}
55
}
66

77
declare type ApiExtension = {

index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Base } from "./index.js";
44
import { fooPlugin } from "./plugins/foo/index.js";
55
import { barPlugin } from "./plugins/bar/index.js";
66
import { voidPlugin } from "./plugins/void/index.js";
7+
import { withOptionsPlugin } from "./plugins/with-options";
78

89
const base = new Base();
910

@@ -48,3 +49,8 @@ expectType<string>(baseWithVoidAndNonVoidPlugins.bar);
4849

4950
// @ts-expect-error unknown properties cannot be used, see #31
5051
baseWithVoidAndNonVoidPlugins.unknown;
52+
53+
const BaseWithOptionsPlugin = Base.plugin(withOptionsPlugin);
54+
const baseWithOptionsPlugin = new BaseWithOptionsPlugin();
55+
56+
expectType<string>(baseWithOptionsPlugin.getFooOption());

plugins/with-options/index.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-plugin-d-ts.html
2+
import { Base } from "../..";
3+
4+
declare module "../.." {
5+
namespace Base {
6+
interface Options {
7+
foo?: string;
8+
}
9+
}
10+
}
11+
12+
export function withOptionsPlugin(
13+
base: Base,
14+
options: Base.Options
15+
): {
16+
getFooOption: () => Required<Base.Options>["foo"];
17+
};

plugins/with-options/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {import('../..').Base} base
3+
* @param {import('../..').Base.Options} options
4+
*/
5+
export function withOptionsPlugin(base, options) {
6+
return {
7+
getFooOption() {
8+
return options.foo || "my default";
9+
},
10+
};
11+
}

0 commit comments

Comments
 (0)