diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index 43fc11ea1..064c33062 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -13,7 +13,7 @@ import Root from './root.js' * const lazy = postcss([autoprefixer]).process(css) * ``` */ -export default class LazyResult { +export default class LazyResult implements Promise { /** * Processes input CSS through synchronous and asynchronous plugins * and calls `onFulfilled` with a Result instance. If a plugin throws @@ -66,6 +66,12 @@ export default class LazyResult { */ constructor (processor: Processor, css: string, opts: ResultOptions) + /** + * Returns the default string description of an object. + * Required to implement the Promise interface. + */ + get [Symbol.toStringTag] (): string + /** * Returns a `Processor` instance, which will be used * for CSS transformations. diff --git a/lib/lazy-result.js b/lib/lazy-result.js index b339bc008..97bea522d 100644 --- a/lib/lazy-result.js +++ b/lib/lazy-result.js @@ -114,6 +114,10 @@ class LazyResult { }) } + get [Symbol.toStringTag] () { + return 'LazyResult' + } + get processor () { return this.result.processor } diff --git a/test/lazy-result.test.ts b/test/lazy-result.test.ts index 72babcd84..20ad20b0c 100644 --- a/test/lazy-result.test.ts +++ b/test/lazy-result.test.ts @@ -57,3 +57,8 @@ it('executes on finally callback', () => { .finally(mockCallback) .then(() => expect(mockCallback).toHaveBeenCalledTimes(1)) }) + +it('prints its object type', () => { + let result = new LazyResult(processor, 'a {}', {}) + expect(Object.prototype.toString.call(result)).toEqual('[object LazyResult]') +}) diff --git a/test/types.ts b/test/types.ts index 34f71381b..606b5f1fd 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,4 +1,4 @@ -import { PluginCreator } from '../lib/postcss.js' +import postcss, { Result, PluginCreator, SourceMap } from '../lib/postcss.js' const plugin: PluginCreator = prop => { return { @@ -14,4 +14,19 @@ const plugin: PluginCreator = prop => { plugin.postcss = true +interface StyleCompileResults { + code: string + map: SourceMap | undefined +} + +const processResult: Promise | Result = postcss([ + plugin +]).process('h1{color: black;}', { from: undefined }) +const processed: + | StyleCompileResults + | Promise = processResult.then(result => ({ + code: result.css, + map: result.map +})) + export default plugin