Browse Source

@uppy/core: add `PluginTarget` type and mark options as optional (#4874)

Antoine du Hamel 1 năm trước cách đây
mục cha
commit
d861fcd597
2 tập tin đã thay đổi với 15 bổ sung6 xóa
  1. 2 2
      packages/@uppy/core/src/BasePlugin.ts
  2. 13 4
      packages/@uppy/core/src/UIPlugin.ts

+ 2 - 2
packages/@uppy/core/src/BasePlugin.ts

@@ -43,9 +43,9 @@ export default class BasePlugin<
 
   VERSION: string
 
-  constructor(uppy: Uppy<M, B>, opts: Opts) {
+  constructor(uppy: Uppy<M, B>, opts?: Partial<Opts>) {
     this.uppy = uppy
-    this.opts = opts ?? {}
+    this.opts = (opts ?? {}) as Opts
   }
 
   getPluginState(): PluginState {

+ 13 - 4
packages/@uppy/core/src/UIPlugin.ts

@@ -60,7 +60,9 @@ class UIPlugin<
 
   title: string
 
-  getTargetPlugin(target: unknown): UIPlugin<any, any, any> | undefined {
+  getTargetPlugin<Me extends Meta, Bo extends Body>(
+    target: PluginTarget<Me, Bo>, // eslint-disable-line no-use-before-define
+  ): UIPlugin<any, Me, Bo> | undefined {
     let targetPlugin
     if (typeof target === 'object' && target instanceof UIPlugin) {
       // Targeting a plugin *instance*
@@ -84,9 +86,9 @@ class UIPlugin<
    * If it’s an object — target is a plugin, and we search `plugins`
    * for a plugin with same name and return its target.
    */
-  mount(
-    target: HTMLElement | string,
-    plugin: UIPlugin<any, any, any>,
+  mount<Me extends Meta, Bo extends Body>(
+    target: PluginTarget<Me, Bo>, // eslint-disable-line no-use-before-define
+    plugin: UIPlugin<any, Me, Bo>,
   ): HTMLElement {
     const callerPluginName = plugin.id
 
@@ -196,3 +198,10 @@ class UIPlugin<
 }
 
 export default UIPlugin
+
+export type PluginTarget<M extends Meta, B extends Body> =
+  | string
+  | Element
+  | typeof BasePlugin
+  | typeof UIPlugin
+  | BasePlugin<any, M, B>