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

@uppy/core: improve types of .use() #4882

Merged
merged 3 commits into from
Jan 23, 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
29 changes: 29 additions & 0 deletions packages/@uppy/core/src/Uppy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import assert from 'node:assert'
import fs from 'node:fs'
import path from 'node:path'
import prettierBytes from '@transloadit/prettier-bytes'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
import Core from './index.ts'
import UIPlugin from './UIPlugin.ts'
import BasePlugin, { type PluginOpts } from './BasePlugin.ts'
import { debugLogger } from './loggers.ts'
import AcquirerPlugin1 from './mocks/acquirerPlugin1.ts'
import AcquirerPlugin2 from './mocks/acquirerPlugin2.ts'
Expand Down Expand Up @@ -61,6 +63,33 @@ describe('src/Core', () => {
).toEqual(1)
})

it('should be able to .use() without passing generics again', () => {
interface TestOpts extends PluginOpts {
foo?: string
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
bar: string
}
class TestPlugin<M extends Meta, B extends Body> extends BasePlugin<
TestOpts,
M,
B
> {
foo: string

constructor(uppy: Core<M, B>, opts: TestOpts) {
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
super(uppy, opts)
this.id = 'Test'
this.type = 'acquirer'
this.foo = opts?.foo ?? 'bar'
}
}
new Core().use(TestPlugin)
new Core().use(TestPlugin, { foo: '', bar: '' })
// @ts-expect-error boolean not allowed
new Core().use(TestPlugin, { foo: false })
// @ts-expect-error missing option
new Core().use(TestPlugin, { foo: '' })
})

it('should prevent the same plugin from being added more than once', () => {
const core = new Core()
core.use(AcquirerPlugin1)
Expand Down
7 changes: 3 additions & 4 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import locale from './locale.ts'
import type BasePlugin from './BasePlugin.ts'
import type UIPlugin from './UIPlugin.ts'
import type { Restrictions } from './Restricter.ts'
import type { PluginOpts } from './BasePlugin.ts'

type Processor = (fileIDs: string[], uploadID: string) => Promise<void> | void

Expand Down Expand Up @@ -1663,9 +1662,9 @@ export class Uppy<M extends Meta, B extends Body> {
/**
* Registers a plugin with Core.
*/
use<O extends PluginOpts, I extends UIPlugin<O, M, B> | BasePlugin<O, M, B>>(
Plugin: new (uppy: this, opts?: O) => I,
opts?: O,
use<T extends typeof BasePlugin<any, M, B> | typeof UIPlugin<any, M, B>>(
Plugin: T,
opts?: ConstructorParameters<T>[1],
): this {
if (typeof Plugin !== 'function') {
const msg =
Expand Down
Loading