ソースを参照

@uppy/core: use variadic arguments for `uppy.use` (#4888)

Antoine du Hamel 1 年間 前
コミット
09e6a0fe53

+ 1 - 0
packages/@uppy/core/src/Uppy.test.ts

@@ -88,6 +88,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

+ 7 - 2
packages/@uppy/core/src/Uppy.ts

@@ -62,6 +62,9 @@ export type UnknownPlugin<
   PluginState extends Record<string, unknown> = Record<string, unknown>,
 > = BasePlugin<any, M, B, PluginState>
 
+// `OmitFirstArg<typeof someArray>` is the type of the returned value of `someArray.slice(1)`.
+type OmitFirstArg<T> = T extends [any, ...infer U] ? U : never
+
 export type UnknownProviderPluginState = {
   authenticated: boolean | undefined
   breadcrumbs: {
@@ -1718,7 +1721,9 @@ export class Uppy<M extends Meta, B extends Body> {
    */
   use<T extends typeof BasePlugin<any, M, B>>(
     Plugin: T,
-    opts?: ConstructorParameters<T>[1],
+    // 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<ConstructorParameters<T>>
   ): this {
     if (typeof Plugin !== 'function') {
       const msg =
@@ -1730,7 +1735,7 @@ export class Uppy<M extends Meta, B extends Body> {
     }
 
     // Instantiate
-    const plugin = new Plugin(this, opts)
+    const plugin = new Plugin(this, ...args)
     const pluginId = plugin.id
 
     if (!pluginId) {

+ 1 - 1
packages/@uppy/core/src/mocks/acquirerPlugin1.ts

@@ -9,7 +9,7 @@ export default class TestSelector1 extends UIPlugin<any, any, any> {
 
   mocks: { run: mock; update: mock; uninstall: mock }
 
-  constructor(uppy: Uppy<any, any>, opts: any) {
+  constructor(uppy: Uppy<any, any>, opts?: any) {
     super(uppy, opts)
     this.type = 'acquirer'
     this.id = 'TestSelector1'

+ 1 - 1
packages/@uppy/core/src/mocks/acquirerPlugin2.ts

@@ -9,7 +9,7 @@ export default class TestSelector2 extends UIPlugin<any, any, any> {
 
   mocks: { run: mock; update: mock; uninstall: mock }
 
-  constructor(uppy: Uppy<any, any>, opts: any) {
+  constructor(uppy: Uppy<any, any>, opts?: any) {
     super(uppy, opts)
     this.type = 'acquirer'
     this.id = 'TestSelector2'

+ 1 - 1
packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts

@@ -6,7 +6,7 @@ export default class InvalidPluginWithoutName extends UIPlugin<any, any, any> {
 
   public name: string
 
-  constructor(uppy: Uppy<any, any>, opts: any) {
+  constructor(uppy: Uppy<any, any>, opts?: any) {
     super(uppy, opts)
     this.type = 'acquirer'
     this.name = this.constructor.name

+ 1 - 1
packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts

@@ -6,7 +6,7 @@ export default class InvalidPluginWithoutType extends UIPlugin<any, any, any> {
 
   public name: string
 
-  constructor(uppy: Uppy<any, any>, opts: any) {
+  constructor(uppy: Uppy<any, any>, opts?: any) {
     super(uppy, opts)
     this.id = 'InvalidPluginWithoutType'
     this.name = this.constructor.name