From 1165c8e1476f91c927b318dd8d6c657bb2707011 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Sun, 13 Jun 2021 14:46:55 -0700 Subject: [PATCH] fix: avoid `base` to be set to an intersection with `void` if a plugin returns `void` --- index.d.ts | 2 +- index.test-d.ts | 50 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6f115be..3c3cadf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -21,7 +21,7 @@ declare type ReturnTypeOf = T extends AnyFunction ? ReturnType : T extends AnyFunction[] - ? UnionToIntersection> + ? UnionToIntersection, void>> : never; export declare class Base { diff --git a/index.test-d.ts b/index.test-d.ts index 6513b46..4ce594b 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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(base.options.default); -expectType(base.options.option); -expectType(base.foo()); +expectType(fooBase.options.default); +expectType(fooBase.options.option); +expectType(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(baseWithFooAndBarPlugins.foo); +expectType(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(baseWithVoidAndNonVoidPlugins.foo); +expectType(baseWithVoidAndNonVoidPlugins.bar);