[loadjs] v4 definitions (#38941)

This commit is contained in:
Aziz Khambati 2019-10-08 05:13:37 +05:30 committed by Armando Aguirre
parent b8a7daa927
commit ca5aab8759
4 changed files with 61 additions and 19 deletions

View File

@ -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 <https://github.com/screendriver>
// Aziz Khambati <https://github.com/azizhk>
// 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<void>;
declare function loadjs(
files: string | string[],
bundleId: string,
options: loadjs.LoadOptions & {
returnPromise: true
}
): Promise<void>;
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;

View File

@ -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<void>
importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], { ...loadOptions, returnPromise: true }); // $ExpectType Promise<void>
importedLoadJs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { ...loadOptions, returnPromise: true }); // $ExpectType Promise<void>
importedLoadJs('/path/to/foo.js', 'foo', { ...loadOptions, returnPromise: true }); // $ExpectType Promise<void>

View File

@ -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(() => {});

View File

@ -2,7 +2,8 @@
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,