diff --git a/types/clean-css/clean-css-tests.ts b/types/clean-css/clean-css-tests.ts index 9c856d40a1..6c92a5ae88 100644 --- a/types/clean-css/clean-css-tests.ts +++ b/types/clean-css/clean-css-tests.ts @@ -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); + } +); \ No newline at end of file diff --git a/types/clean-css/index.d.ts b/types/clean-css/index.d.ts index 244c585e53..f806397540 100644 --- a/types/clean-css/index.d.ts +++ b/types/clean-css/index.d.ts @@ -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; } + /** + * 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; } }