Split options to improve signature recongition

This commit is contained in:
Andrew Potter 2019-01-29 16:28:58 -07:00
parent 2a1fd4d999
commit edd2f71621
2 changed files with 55 additions and 12 deletions

View File

@ -81,3 +81,32 @@ new CleanCSS({ returnPromise: true, sourceMap: true }).minify(source, inputSourc
console.log(error);
}
);
// test object return when passing options as object
let CleanCssOptions: CleanCSS.Options = { returnPromise: true };
new CleanCSS(CleanCssOptions).minify(source)
.then((minified: CleanCSS.Output): void => {
console.log(minified.styles);
}).catch((error: any): void => {
console.log(error);
}
);
CleanCssOptions = { returnPromise: false };
new CleanCSS(CleanCssOptions).minify(source, (error: any, minified: CleanCSS.Output): void => {
console.log(minified.styles);
});
// type conversion
CleanCssOptions = {};
// in this case, the compiler will think its an OptionsOutput
// so if we want to make it a promise, we will need to cast it specifically its a promise type return
(CleanCssOptions = CleanCssOptions as CleanCSS.OptionsPromise).returnPromise = true;
new CleanCSS(CleanCssOptions).minify(source)
.then((minified: CleanCSS.Output): void => {
console.log(minified.styles);
}).catch((error: any): void => {
console.log(error);
}
);

View File

@ -9,15 +9,16 @@ import { RequestOptions as HttpsRequestOptions } from "https";
import { RequestOptions as HttpRequestOptions } from "http";
declare namespace CleanCSS {
/**
* Options passed when initializing a new instance of CleanCSS
* Shared options passed when initializing a new instance of CleanCSS that returns either a promise or output
*/
interface Options {
interface OptionsBase {
/**
* Controls compatibility mode used; defaults to ie10+ using `'*'`.
* Compatibility hash exposes the following properties: `colors`, `properties`, `selectors`, and `units`
*/
compatibility?: "*" | "ie9" | "ie8" | "ie7" | CompatibilityOptions;
compatibility?: "*" | "ie9" | "ie8" | "ie7" | CleanCSS.CompatibilityOptions;
/**
* Controls a function for handling remote requests; Defaults to the build in `loadRemoteResource` function
@ -28,7 +29,7 @@ declare namespace CleanCSS {
* Controls output CSS formatting; defaults to `false`.
* Format hash exposes the following properties: `breaks`, `breakWith`, `indentBy`, `indentWith`, `spaces`, and `wrapAt`.
*/
format?: "beautify" | "keep-breaks" | FormatOptions | false;
format?: "beautify" | "keep-breaks" | CleanCSS.FormatOptions | false;
/**
* inline option whitelists which @import rules will be processed. Defaults to `'local'`
@ -56,7 +57,7 @@ declare namespace CleanCSS {
* Controls optimization level used; defaults to `1`.
* Level hash exposes `1`, and `2`.
*/
level?: 0 | 1 | 2 | OptimizationsOptions;
level?: 0 | 1 | 2 | CleanCSS.OptimizationsOptions;
/**
* Controls URL rebasing; defaults to `true`;
@ -69,11 +70,6 @@ declare namespace CleanCSS {
*/
rebaseTo?: string;
/**
* If you prefer clean-css to return a Promise object then you need to explicitely ask for it; defaults to `false`
*/
returnPromise?: boolean;
/**
* Controls whether an output source map is built; defaults to `false`
*/
@ -650,12 +646,30 @@ declare namespace CleanCSS {
minify(sources: Sources, sourceMap?: string): Promise<Output>;
}
/**
* Options when returning a promise
*/
type OptionsPromise = OptionsBase & { returnPromise: true };
/**
* Options when returning an output
*/
type OptionsOutput = OptionsBase & { returnPromise?: false };
/**
* Discriminant union of both sets of options types. If you initialize without setting `returnPromise: true`
* and want to return a promise, you will need to cast to the correct options type so that TypeScript
* knows what the expected return type will be:
* `(options = options as CleanCSS.OptionsPromise).returnPromise = true`
*/
type Options = OptionsPromise | OptionsOutput;
/**
* Constructor interface for CleanCSS
*/
interface Constructor {
new(options: Options & { returnPromise: true }): MinifierPromise;
new(options?: Options): MinifierOutput;
new(options: OptionsPromise): MinifierPromise;
new(options?: OptionsOutput): MinifierOutput;
}
}