diff --git a/packages/@uppy/core/types/core-tests.ts b/packages/@uppy/core/types/core-tests.ts index 6aefd0470..c77eb35a6 100644 --- a/packages/@uppy/core/types/core-tests.ts +++ b/packages/@uppy/core/types/core-tests.ts @@ -50,3 +50,22 @@ import DefaultStore = require('@uppy/store-default') meta: { path: 'path/to/file' } }) } + +{ + const uppy = Uppy() + // can emit events with internal event types + uppy.emit('upload') + uppy.emit('complete', () => {}) + uppy.emit('error', () => {}) + + // can emit events with custom event types + uppy.emit('dashboard:modal-closed', () => {}) + + // can register listners for internal events + uppy.on('upload', () => {}) + uppy.on('complete', () => {}) + uppy.on('error', () => {}) + + // can register listners on custom events + uppy.on('dashboard:modal-closed', () => {}) +} diff --git a/packages/@uppy/core/types/index.d.ts b/packages/@uppy/core/types/index.d.ts index eec10f336..c4d4afa47 100644 --- a/packages/@uppy/core/types/index.d.ts +++ b/packages/@uppy/core/types/index.d.ts @@ -99,16 +99,23 @@ declare module Uppy { totalProgress: number; } type LogLevel = 'info' | 'warning' | 'error'; + + // This hack accepts _any_ string for `Event`, but also tricks VSCode and friends into providing autocompletions + // for the names listed. https://github.com/microsoft/TypeScript/issues/29729#issuecomment-505826972 + type LiteralUnion = T | (U & { }); + type Event = LiteralUnion<'file-added' | 'file-removed' | 'upload' | 'upload-progress' | 'upload-success' | 'complete' | 'error' | 'upload-error' | + 'upload-retry' | 'info-visible' | 'info-hidden' | 'cancel-all' | 'restriction-failed' | 'reset-progress'>; + class Uppy { constructor(opts?: Partial); on = {}>(event: 'upload-success', callback: (file: UppyFile, body: any, uploadURL: string) => void): Uppy; on = {}>(event: 'complete', callback: (result: UploadResult) => void): Uppy; - on(event: string, callback: (...args: any[]) => void): Uppy; - off(event: string, callback: any): Uppy; + on(event: Event, callback: (...args: any[]) => void): Uppy; + off(event: Event, callback: any): Uppy; /** * For use by plugins only! */ - emit(event: string, ...args: any[]): void; + emit(event: Event, ...args: any[]): void; updateAll(state: object): void; setState(patch: object): void; getState = {}>(): State;