Skip to content

Commit

Permalink
feat: inher constructor options via Base.defaults()
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jun 18, 2021
1 parent 4c8cec9 commit bf15c10
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 4 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export declare namespace Base {
type Options = {
interface Options {
version: string;
[key: string]: unknown;
};
}
}

declare type ApiExtension = {
Expand Down Expand Up @@ -56,7 +57,7 @@ export declare class Base<TOptions extends Base.Options = Base.Options> {
options: TDefaults;
};
} & S;
constructor(options?: TOptions);
constructor(options: TOptions);
options: TOptions;
}
export {};
17 changes: 13 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { fooPlugin } from "./plugins/foo/index.js";
import { barPlugin } from "./plugins/bar/index.js";
import { voidPlugin } from "./plugins/void/index.js";

const base = new Base();
const base = new Base({
version: "1.2.3",
});

// @ts-expect-error unknown properties cannot be used, see #31
base.unknown;

const FooBase = Base.plugin(fooPlugin).defaults({
default: "value",
version: "1.2.3",
});
const fooBase = new FooBase({
option: "value",
Expand All @@ -22,13 +25,17 @@ expectType<string>(fooBase.options.option);
expectType<string>(fooBase.foo);

const BaseWithVoidPlugin = Base.plugin(voidPlugin);
const baseWithVoidPlugin = new BaseWithVoidPlugin();
const baseWithVoidPlugin = new BaseWithVoidPlugin({
version: "1.2.3",
});

// @ts-expect-error unknown properties cannot be used, see #31
baseWithVoidPlugin.unknown;

const BaseWithFooAndBarPlugins = Base.plugin(barPlugin, fooPlugin);
const baseWithFooAndBarPlugins = new BaseWithFooAndBarPlugins();
const baseWithFooAndBarPlugins = new BaseWithFooAndBarPlugins({
version: "1.2.3",
});

expectType<string>(baseWithFooAndBarPlugins.foo);
expectType<string>(baseWithFooAndBarPlugins.bar);
Expand All @@ -41,7 +48,9 @@ const BaseWithVoidAndNonVoidPlugins = Base.plugin(
voidPlugin,
fooPlugin
);
const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins();
const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins({
version: "1.2.3",
});

expectType<string>(baseWithVoidAndNonVoidPlugins.foo);
expectType<string>(baseWithVoidAndNonVoidPlugins.bar);
Expand Down

0 comments on commit bf15c10

Please sign in to comment.