Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"types": "./index.d.ts",
"scripts": {
"build": "tsc src/index.ts --declaration --outDir .",
"test": "jest --coverage"
"test": "npm run -s test:code && npm run -s test:typescript",
"test:code": "jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals test-typescript.ts"
},
"repository": "github:gr2m/javascript-plugin-architecture-with-typescript-definitions",
"keywords": [
Expand Down
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> = T extends AnyFunction
? UnionToIntersection<ReturnType<T[number]>>
: never;

export class Base {
export class Base<TOptions extends Options = Options> {
static plugins: TestPlugin[] = [];
static plugin<
S extends Constructor<any> & { plugins: any[] },
Expand All @@ -46,21 +46,25 @@ export class Base {
);
};

return BaseWithPlugins as typeof this & { plugins: any[] } &
Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
return BaseWithPlugins as typeof this & { plugins: any[] } & Constructor<
UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>
>;
}

static defaults<S extends Constructor<any>>(this: S, defaults: Options) {
static defaults<
TDefaults extends Options,
S extends Constructor<Base<TDefaults>>
>(this: S, defaults: TDefaults) {
const BaseWitDefaults = class extends this {
constructor(...args: any[]) {
super(Object.assign({}, defaults, args[0] || {}));
}
};

return BaseWitDefaults as typeof this;
return BaseWitDefaults as typeof BaseWitDefaults & typeof this;
}

constructor(options: Options = {}) {
constructor(options: TOptions = {} as TOptions) {
this.options = options;

// apply plugins
Expand All @@ -71,5 +75,5 @@ export class Base {
});
}

options: Options;
}
options: TOptions;
}
22 changes: 22 additions & 0 deletions test-typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ************************************************************
// THIS CODE IS NOT EXECUTED. IT IS JUST FOR TYPECHECKING
// ************************************************************

import { Base } from "./src";

function isString(what: string) {}

// sets .options types from constructor options
const base = new Base({ option: "value" });
isString(base.options.option);

// sets .options types from Base.defaults({})
const BaseWithDefaults = Base.defaults({ parentOption: "value" });
const baseWithDefaults = new BaseWithDefaults({ childOption: "value" });

isString(baseWithDefaults.options.childOption);
// see #32
isString(baseWithDefaults.options.parentOption);

// @ts-expect-error see #31
baseWithDefaults.unknown;