Skip to content

Commit

Permalink
fix(typescript): ensure LazyResult complies with Promise interface.
Browse files Browse the repository at this point in the history
* Explicitly implement Promise<Result> in the type declaration
* Add mising [Symbol.toStringTag] property
* Add some type tests to ensure we can type `postcss(plugins).process` results
  • Loading branch information
ludofischer committed Nov 18, 2020
1 parent f8fe285 commit 34d765d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/lazy-result.d.ts
Expand Up @@ -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<Result> {
/**
* Processes input CSS through synchronous and asynchronous plugins
* and calls `onFulfilled` with a Result instance. If a plugin throws
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion 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<string> = prop => {
return {
Expand All @@ -14,4 +14,19 @@ const plugin: PluginCreator<string> = prop => {

plugin.postcss = true

interface StyleCompileResults {
code: string
map: SourceMap | undefined
}

const processResult: Promise<Result> | Result = postcss([
plugin
]).process('h1{color: black;}', { from: undefined })
const processed:
| StyleCompileResults
| Promise<StyleCompileResults> = processResult.then(result => ({
code: result.css,
map: result.map
}))

export default plugin

0 comments on commit 34d765d

Please sign in to comment.