Skip to content

fix: avoid base to be set to an intersection with void if a plugin returns void #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2021
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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> =
T extends AnyFunction
? ReturnType<T>
: T extends AnyFunction[]
? UnionToIntersection<ReturnType<T[number]>>
? UnionToIntersection<Exclude<ReturnType<T[number]>, void>>
: never;

export declare class Base<TOptions extends Options = Options> {
Expand Down
50 changes: 43 additions & 7 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,56 @@ import { Base } from "./index.js";

const fooPlugin = () => {
return {
foo: () => "foo",
foo: "foo",
};
};
const barPlugin = () => {
return {
bar: "bar",
};
};

const voidPlugin = () => {
// returns void
};

const base = new Base();

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

const MyBase = Base.plugin(fooPlugin).defaults({
const FooBase = Base.plugin(fooPlugin).defaults({
default: "value",
});
const base = new MyBase({
const fooBase = new FooBase({
option: "value",
});

expectType<string>(base.options.default);
expectType<string>(base.options.option);
expectType<string>(base.foo());
expectType<string>(fooBase.options.default);
expectType<string>(fooBase.options.option);
expectType<string>(fooBase.foo);

const BaseWithVoidPlugin = Base.plugin(voidPlugin);
const baseWithVoidPlugin = new BaseWithVoidPlugin();

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

const BaseWithFooAndBarPlugins = Base.plugin(barPlugin, fooPlugin);
const baseWithFooAndBarPlugins = new BaseWithFooAndBarPlugins();

expectType<string>(baseWithFooAndBarPlugins.foo);
expectType<string>(baseWithFooAndBarPlugins.bar);

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

const BaseWithVoidAndNonVoidPlugins = Base.plugin(
barPlugin,
voidPlugin,
fooPlugin
);
const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins();

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