Skip to content
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

fix(core): Allow arrays of plugins on usePlugin(..) #116

Merged
merged 1 commit into from
Jan 26, 2024
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
11 changes: 7 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ export {
};

/**
* Extends `@assertive-ts/core` with a local or 3rd-party plugin.
* Extends `@assertive-ts/core` with local or 3rd-party plugin(s).
*
* @param plugin the plugin to use to extend assertive-ts
* @param plugins a plugin or an array of plugins to use
* @see {@link Plugin Plugin}
*/
export function usePlugin<T, A extends Assertion<T>>(plugin: Plugin<T, A>): void {
config.addPlugin(plugin);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function usePlugin<P extends Plugin<any, Assertion<any>>>(plugins: P | P[]): void {
Comment on lines +28 to +29
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish there were a better way to get rid of these any, but without them, the argument cannot accept an array of union types. For example, [Plugin<X, XAssertion>, Plugin<Y, YAssertion>] will not work.

Array.isArray(plugins)
? plugins.forEach(plugin => config.addPlugin(plugin))
: config.addPlugin(plugins);
}
8 changes: 7 additions & 1 deletion packages/core/src/lib/config/Config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Assertion } from "../Assertion";

/**
* A plugin object that can be used to extend the `expect(..)` function.
*
* @param T the type the plugin is meant for
* @param A the type of the assertion for `T`
*/
export interface Plugin<T, A extends Assertion<T>> {
/**
* The `Assertion<T>` instance the plugin adds
* The assertion `A` constructor the plugin adds
*/
Assertion: new(actual: T) => A;
/**
Expand Down