Selaa lähdekoodia

core: add typings for `setOptions()`. (#2135)

* core: add typings for `setOptions()`.

* core: add typings test for plugin#setOptions
Renée Kooi 5 vuotta sitten
vanhempi
commit
0aa511a476

+ 3 - 1
packages/@uppy/core/types/index.d.ts

@@ -53,6 +53,7 @@ declare module Uppy {
     uppy: Uppy
     type: string
     constructor(uppy: Uppy, opts?: TOptions)
+    setOptions(update: Partial<TOptions>): void
     getPluginState(): object
     setPluginState(update: IndexedObject<any>): object
     update(state?: object): void
@@ -156,10 +157,11 @@ declare module Uppy {
     on(event: Event, callback: (...args: any[]) => void): this
     off(event: Event, callback: (...args: any[]) => void): this
     /**
-     * For use by plugins only!
+     * For use by plugins only.
      */
     emit(event: Event, ...args: any[]): void
     updateAll(state: object): void
+    setOptions(update: Partial<UppyOptions>): void
     setState(patch: object): void
     getState<TMeta extends IndexedObject<any> = {}>(): State<TMeta>
     readonly state: State

+ 23 - 0
packages/@uppy/core/types/index.test-d.ts

@@ -89,3 +89,26 @@ import DefaultStore = require('@uppy/store-default')
   // can register listners on custom events
   uppy.on('dashboard:modal-closed', () => {})
 }
+
+{
+  const uppy = Uppy()
+  uppy.setOptions({
+    restrictions: {
+      allowedFileTypes: ['.png']
+    }
+  })
+  expectError(uppy.setOptions({ restrictions: false }))
+  expectError(uppy.setOptions({ unknownKey: false }))
+}
+
+{
+  interface TestOptions extends Uppy.PluginOptions {
+    testOption: string
+  }
+  class TestPlugin extends Uppy.Plugin<TestOptions> {}
+
+  const strict = Uppy<Uppy.StrictTypes>().use(TestPlugin, { testOption: 'hello' })
+  ;(strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ testOption: 'world' })
+  expectError((strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ testOption: 0 }))
+  expectError((strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ unknownKey: false }))
+}