Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): ensure LazyResult complies with Promise interface. #1463

Merged
merged 1 commit into from Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
ai marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a `Processor` instance, which will be used
* for CSS transformations.
Expand Down
4 changes: 4 additions & 0 deletions lib/lazy-result.js
Expand Up @@ -114,6 +114,10 @@ class LazyResult {
})
}

get [Symbol.toStringTag] () {
return 'LazyResult'
}

get processor () {
return this.result.processor
}
Expand Down
5 changes: 5 additions & 0 deletions test/lazy-result.test.ts
Expand Up @@ -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]')
})
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we can use processed: Result = here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am not sure I understand the question. I don't think processed can be of type result since I am mapping it to a different object in then(). If you want I can simplify this example, it actually comes from a real-life sample in the Vue.js 3 repo).

| StyleCompileResults
| Promise<StyleCompileResults> = processResult.then(result => ({
code: result.css,
map: result.map
}))

export default plugin