From e4a5c95f2517a8c6e81823b9daecd1af2791d209 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 23 Jan 2024 15:24:02 +0100 Subject: [PATCH 1/4] @uppy/core: use variadic arguments for `uppy.use` --- packages/@uppy/core/src/Uppy.test.ts | 1 + packages/@uppy/core/src/Uppy.ts | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/@uppy/core/src/Uppy.test.ts b/packages/@uppy/core/src/Uppy.test.ts index 4d097f0263..949d969ce9 100644 --- a/packages/@uppy/core/src/Uppy.test.ts +++ b/packages/@uppy/core/src/Uppy.test.ts @@ -89,6 +89,7 @@ describe('src/Core', () => { this.bar = this.opts.bar } } + // @ts-expect-error missing mandatory option foo new Core().use(TestPlugin) new Core().use(TestPlugin, { foo: '', bar: '' }) // @ts-expect-error boolean not allowed diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 9d7d1aadc1..0bb186f51b 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -48,6 +48,8 @@ type UnknownPlugin = InstanceType< typeof BasePlugin | typeof UIPlugin > +type OmitFirstArg = T extends [any, ...infer U] ? U : never + type UnknownProviderPlugin = UnknownPlugin< M, B @@ -1653,7 +1655,7 @@ export class Uppy { */ use>( Plugin: T, - opts?: ConstructorParameters[1], + ...args: OmitFirstArg> ): this { if (typeof Plugin !== 'function') { const msg = @@ -1665,7 +1667,7 @@ export class Uppy { } // Instantiate - const plugin = new Plugin(this, opts) + const plugin = new Plugin(this, ...args) const pluginId = plugin.id if (!pluginId) { From 91ad4c414cdd491b9014026827c2fc25568f547f Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 23 Jan 2024 16:28:24 +0100 Subject: [PATCH 2/4] fix types --- packages/@uppy/core/src/mocks/acquirerPlugin1.ts | 2 +- packages/@uppy/core/src/mocks/acquirerPlugin2.ts | 2 +- packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts | 2 +- packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/@uppy/core/src/mocks/acquirerPlugin1.ts b/packages/@uppy/core/src/mocks/acquirerPlugin1.ts index 2968f37849..0f3516690d 100644 --- a/packages/@uppy/core/src/mocks/acquirerPlugin1.ts +++ b/packages/@uppy/core/src/mocks/acquirerPlugin1.ts @@ -9,7 +9,7 @@ export default class TestSelector1 extends UIPlugin { mocks: { run: mock; update: mock; uninstall: mock } - constructor(uppy: Uppy, opts: any) { + constructor(uppy: Uppy, opts?: any) { super(uppy, opts) this.type = 'acquirer' this.id = 'TestSelector1' diff --git a/packages/@uppy/core/src/mocks/acquirerPlugin2.ts b/packages/@uppy/core/src/mocks/acquirerPlugin2.ts index 01a5c2fa1c..9d2290b69d 100644 --- a/packages/@uppy/core/src/mocks/acquirerPlugin2.ts +++ b/packages/@uppy/core/src/mocks/acquirerPlugin2.ts @@ -9,7 +9,7 @@ export default class TestSelector2 extends UIPlugin { mocks: { run: mock; update: mock; uninstall: mock } - constructor(uppy: Uppy, opts: any) { + constructor(uppy: Uppy, opts?: any) { super(uppy, opts) this.type = 'acquirer' this.id = 'TestSelector2' diff --git a/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts b/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts index f24dc4b8f0..d2c02e30dc 100644 --- a/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts +++ b/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts @@ -6,7 +6,7 @@ export default class InvalidPluginWithoutName extends UIPlugin { public name: string - constructor(uppy: Uppy, opts: any) { + constructor(uppy: Uppy, opts?: any) { super(uppy, opts) this.type = 'acquirer' this.name = this.constructor.name diff --git a/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts b/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts index 3e600eaed4..4baeb573c9 100644 --- a/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts +++ b/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts @@ -6,7 +6,7 @@ export default class InvalidPluginWithoutType extends UIPlugin { public name: string - constructor(uppy: Uppy, opts: any) { + constructor(uppy: Uppy, opts?: any) { super(uppy, opts) this.id = 'InvalidPluginWithoutType' this.name = this.constructor.name From c8882989f60571387ccbd93e3158537df4185b65 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 24 Jan 2024 14:30:18 +0100 Subject: [PATCH 3/4] add comments --- packages/@uppy/core/src/Uppy.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 0bb186f51b..493a079af7 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -48,6 +48,7 @@ type UnknownPlugin = InstanceType< typeof BasePlugin | typeof UIPlugin > +// `OmitFirstArg` is the type of the returned value of `someArray.slice(1)`. type OmitFirstArg = T extends [any, ...infer U] ? U : never type UnknownProviderPlugin = UnknownPlugin< @@ -1655,6 +1656,7 @@ export class Uppy { */ use>( Plugin: T, + // We are going to additional arguments to the plugin constructor. ...args: OmitFirstArg> ): this { if (typeof Plugin !== 'function') { From ce3784cad2d12a28a6778e877ee0f8808ec28abe Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 24 Jan 2024 15:02:31 +0100 Subject: [PATCH 4/4] Update packages/@uppy/core/src/Uppy.ts Co-authored-by: Merlijn Vos --- packages/@uppy/core/src/Uppy.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index 493a079af7..f97791c5cf 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -1656,7 +1656,8 @@ export class Uppy { */ use>( Plugin: T, - // We are going to additional arguments to the plugin constructor. + // We want to let the plugin decide whether `opts` is optional or not + // so we spread the argument rather than defining `opts:` ourselves. ...args: OmitFirstArg> ): this { if (typeof Plugin !== 'function') {