From ca5aab8759fc351665cb97b8afd759cd45587263 Mon Sep 17 00:00:00 2001 From: Aziz Khambati Date: Tue, 8 Oct 2019 05:13:37 +0530 Subject: [PATCH] [loadjs] v4 definitions (#38941) --- types/loadjs/index.d.ts | 38 ++++++++++++++++++++++----- types/loadjs/test/loadjs-tests.ts | 34 ++++++++++++++++-------- types/loadjs/test/loadjs-umd-tests.ts | 5 ++++ types/loadjs/tsconfig.json | 3 ++- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/types/loadjs/index.d.ts b/types/loadjs/index.d.ts index 9373308815..c928443b8f 100644 --- a/types/loadjs/index.d.ts +++ b/types/loadjs/index.d.ts @@ -1,6 +1,7 @@ -// Type definitions for loadjs 3.5 +// Type definitions for loadjs 4.0 // Project: https://github.com/muicss/loadjs // Definitions by: Christian Rackerseder +// Aziz Khambati // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 @@ -8,23 +9,46 @@ export as namespace loadjs; export = loadjs; -declare function loadjs(files: string | string[], bundleIdOrLoaded: string | loadjs.LoadedFn): void; -declare function loadjs(files: string[], bundleId: string, optionsOrCallback: loadjs.LoadOptions | loadjs.LoadedFn): void; +declare function loadjs( + files: string | string[], + bundleIdOrLoaded: string | loadjs.LoadOptions | loadjs.LoadedFn, +): void; + +declare function loadjs( + files: string | string[], + bundleId: string, + optionsOrCallback: loadjs.LoadOptions | loadjs.LoadedFn +): void; + +declare function loadjs( + files: string | string[], + options: loadjs.LoadOptions & { + returnPromise: true + } +): Promise; + +declare function loadjs( + files: string | string[], + bundleId: string, + options: loadjs.LoadOptions & { + returnPromise: true + } +): Promise; declare namespace loadjs { type LoadedFn = () => void; interface LoadOptions { - before?(path: string, scriptEl: string): void; - success?(): void; - error?(depsNotFound: string): void; + before?(path: string, scriptEl: HTMLElement): void; async?: boolean; numRetries?: number; + success?(): void; // Arguments provided are different in case of returnPromise: true / false + error?(depsNotFound: string[]): void; } interface ReadyOptions { success?(): void; - error?(depsNotFound: string): void; + error?(depsNotFound: string[]): void; } function ready(bundleIds: string | string[], optionsOrCallback: ReadyOptions | LoadedFn): typeof loadjs; diff --git a/types/loadjs/test/loadjs-tests.ts b/types/loadjs/test/loadjs-tests.ts index 0b3aad142d..c7de51ecc5 100644 --- a/types/loadjs/test/loadjs-tests.ts +++ b/types/loadjs/test/loadjs-tests.ts @@ -1,23 +1,35 @@ import importedLoadJs = require('loadjs'); const loadOptions: importedLoadJs.LoadOptions = { - before: (path, scriptEl) => {}, + before: (path, scriptEl) => { + path; // $ExpectType string + scriptEl; // $ExpectType HTMLElement + }, success: () => {}, - error: (pathsNotFound) => {}, + error: (pathsNotFound) => { + pathsNotFound; // $ExpectType string[] + }, async: true, numRetries: 3 }; const readyOptions: importedLoadJs.ReadyOptions = { success: () => {}, - error: (depsNotFound) => {}, + error: (depsNotFound) => { + depsNotFound; // $ExpectType string[] + }, }; -importedLoadJs('/path/to/foo.js', () => {}); -importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], () => {}); -importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); -importedLoadJs.ready('foobar', () => {}).ready('foobar', () => {}); -importedLoadJs.ready(['foo', 'bar'], () => {}).ready(['foo', 'bar'], () => {}); -importedLoadJs.isDefined('foobar'); -importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', loadOptions); -importedLoadJs.ready('foobar', readyOptions); +importedLoadJs('/path/to/foo.js', () => {}); // $ExpectType void +importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], () => {}); // $ExpectType void +importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); // $ExpectType void +importedLoadJs.ready('foobar', () => {}).ready('foobar', () => {}); // $ExpectType typeof loadjs +importedLoadJs.ready(['foo', 'bar'], () => {}).ready(['foo', 'bar'], () => {}); // $ExpectType typeof loadjs +importedLoadJs.isDefined('foobar'); // $ExpectType boolean +importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', loadOptions); // $ExpectType void +importedLoadJs.ready('foobar', readyOptions); // $ExpectType typeof loadjs + +importedLoadJs('/path/to/foo.js', { ...loadOptions, returnPromise: true }); // $ExpectType Promise +importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], { ...loadOptions, returnPromise: true }); // $ExpectType Promise +importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { ...loadOptions, returnPromise: true }); // $ExpectType Promise +importedLoadJs('/path/to/foo.js', 'foo', { ...loadOptions, returnPromise: true }); // $ExpectType Promise diff --git a/types/loadjs/test/loadjs-umd-tests.ts b/types/loadjs/test/loadjs-umd-tests.ts index 94ee4f5090..696183f324 100644 --- a/types/loadjs/test/loadjs-umd-tests.ts +++ b/types/loadjs/test/loadjs-umd-tests.ts @@ -15,3 +15,8 @@ loadjs.ready('foobar', { success: () => {}, error: (depsNotFound) => {}, }); + +loadjs('/path/to/foo.js', { returnPromise: true }).then(() => {}); +loadjs('/path/to/foo.js', 'foobar', { returnPromise: true }).then(() => {}); +loadjs(['/path/to/foo.js', '/path/to/bar.js'], { returnPromise: true }).then(() => {}); +loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { returnPromise: true }).then(() => {}); diff --git a/types/loadjs/tsconfig.json b/types/loadjs/tsconfig.json index b60931cce6..2a90c3f539 100644 --- a/types/loadjs/tsconfig.json +++ b/types/loadjs/tsconfig.json @@ -2,7 +2,8 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6" + "es6", + "dom" ], "noImplicitAny": true, "noImplicitThis": true,