Procházet zdrojové kódy

Add types tests for @uppy/core.

Renée Kooi před 6 roky
rodič
revize
1588b9d8bf

+ 1 - 1
package.json

@@ -95,7 +95,7 @@
     "test:acceptance": "npm run test:prepare-ci && wdio test/endtoend/wdio.remote.conf.js",
     "test:acceptance": "npm run test:prepare-ci && wdio test/endtoend/wdio.remote.conf.js",
     "test:acceptance:local": "npm run test:build && wdio test/endtoend/wdio.local.conf.js",
     "test:acceptance:local": "npm run test:build && wdio test/endtoend/wdio.local.conf.js",
     "test:unit": "jest --testPathPattern=./src --coverage",
     "test:unit": "jest --testPathPattern=./src --coverage",
-    "test:type": "tsc -p packages/uppy/types",
+    "test:type": "tsc -p packages/uppy/types && tsc -p packages/@uppy/core/types",
     "test": "npm run lint && npm run test:unit && npm run test:type",
     "test": "npm run lint && npm run test:unit && npm run test:type",
     "test:watch": "jest --watch --testPathPattern=src",
     "test:watch": "jest --watch --testPathPattern=src",
     "travis:deletecache": "travis cache --delete",
     "travis:deletecache": "travis cache --delete",

+ 13 - 0
packages/@uppy/core/types/core-tests.ts

@@ -0,0 +1,13 @@
+import Uppy, { UppyFile } from '../';
+
+{
+  const uppy = Uppy();
+  uppy.addFile({
+    data: new Blob([new ArrayBuffer(1024)], { type: 'application/octet-stream' })
+  });
+
+  uppy.upload().then((result) => {
+    result.successful[0]; // $ExpectType UppyFile
+    result.failed[0]; // $ExpectType UppyFile
+  });
+}

+ 16 - 12
packages/@uppy/core/types/index.d.ts

@@ -1,4 +1,3 @@
-// TODO actually use the properties here
 export interface UppyFile {
 export interface UppyFile {
   data: Blob | File;
   data: Blob | File;
   extension: string;
   extension: string;
@@ -29,6 +28,11 @@ export interface UppyFile {
   uploadURL?: string;
   uploadURL?: string;
 }
 }
 
 
+export interface AddFileOptions extends Partial<UppyFile> {
+  // `.data` is the only required property here.
+  data: Blob | File;
+}
+
 export interface PluginOptions {
 export interface PluginOptions {
   id?: string;
   id?: string;
 }
 }
@@ -71,16 +75,16 @@ export interface UppyOptions {
 }
 }
 
 
 export interface UploadResult {
 export interface UploadResult {
-  successful: Array<UppyFile>;
-  failed: Array<UppyFile>;
+  successful: UppyFile[];
+  failed: UppyFile[];
 }
 }
 
 
 type LogLevel = 'info' | 'warning' | 'error';
 type LogLevel = 'info' | 'warning' | 'error';
 export class Uppy {
 export class Uppy {
   constructor(opts?: Partial<UppyOptions>);
   constructor(opts?: Partial<UppyOptions>);
-  on(event: 'upload-success', callback: (file: UppyFile, body: any, uploadURL: string) => any): Uppy;
+  on(event: 'upload-success', callback: (file: UppyFile, body: any, uploadURL: string) => void): Uppy;
   on(event: 'complete', callback: (result: UploadResult) => void): Uppy;
   on(event: 'complete', callback: (result: UploadResult) => void): Uppy;
-  on(event: string, callback: (...args: any[]) => any): Uppy;
+  on(event: string, callback: (...args: any[]) => void): Uppy;
   off(event: string, callback: any): Uppy;
   off(event: string, callback: any): Uppy;
   updateAll(state: object): void;
   updateAll(state: object): void;
   setState(patch: object): void;
   setState(patch: object): void;
@@ -95,10 +99,10 @@ export class Uppy {
   addUploader(fn: any): void;
   addUploader(fn: any): void;
   removeUploader(fn: any): void;
   removeUploader(fn: any): void;
   setMeta(data: any): void;
   setMeta(data: any): void;
-  setFileMeta(fileID: string, data: any): void;
+  setFileMeta(fileID: string, data: object): void;
   getFile(fileID: string): UppyFile;
   getFile(fileID: string): UppyFile;
   getFiles(): UppyFile[];
   getFiles(): UppyFile[];
-  addFile(file: object): void;
+  addFile(file: AddFileOptions): void;
   removeFile(fileID: string): void;
   removeFile(fileID: string): void;
   pauseResume(fileID: string): boolean;
   pauseResume(fileID: string): boolean;
   pauseAll(): void;
   pauseAll(): void;
@@ -108,18 +112,18 @@ export class Uppy {
   retryUpload(fileID: string): any;
   retryUpload(fileID: string): any;
   reset(): void;
   reset(): void;
   getID(): string;
   getID(): string;
-  use(plugin: typeof Plugin, opts: any): Uppy;
+  use(pluginClass: typeof Plugin, opts: object): Uppy;
   getPlugin(name: string): Plugin;
   getPlugin(name: string): Plugin;
-  iteratePlugins(method: any): void;
+  iteratePlugins(callback: (plugin: Plugin) => void): void;
   removePlugin(instance: Plugin): void;
   removePlugin(instance: Plugin): void;
   close(): void;
   close(): void;
   info(message: string | { message: string; details: string; }, type?: LogLevel, duration?: number): void;
   info(message: string | { message: string; details: string; }, type?: LogLevel, duration?: number): void;
   hideInfo(): void;
   hideInfo(): void;
   log(msg: string, type?: LogLevel): void;
   log(msg: string, type?: LogLevel): void;
   run(): Uppy;
   run(): Uppy;
-  restore(uploadID: string): Promise<any>;
-  addResultData(uploadID: string, data: any): void;
-  upload(): Promise<any>;
+  restore(uploadID: string): Promise<UploadResult>;
+  addResultData(uploadID: string, data: object): void;
+  upload(): Promise<UploadResult>;
 }
 }
 
 
 export default function createUppy(opts?: Partial<UppyOptions>): Uppy;
 export default function createUppy(opts?: Partial<UppyOptions>): Uppy;

+ 27 - 0
packages/@uppy/core/types/tsconfig.json

@@ -0,0 +1,27 @@
+{
+  "compilerOptions": {
+    "target": "esnext",
+    "module": "commonjs",
+    "lib": [
+      "dom",
+      "esnext"
+    ],
+    "noImplicitAny": true,
+    "noImplicitThis": true,
+    "strictNullChecks": true,
+    "baseUrl": "../",
+    "typeRoots": [
+      "../"
+    ],
+    "types": [],
+    "noEmit": true,
+    "esModuleInterop": true,
+    "allowSyntheticDefaultImports": true,
+    "strictFunctionTypes": true,
+    "forceConsistentCasingInFileNames": true
+  },
+  "files": [
+    "index.d.ts",
+    "core-tests.ts"
+  ]
+}