0

In below type definitions for gulp-postcss on parser?: Object; play any npm-library could be. How I should to annotate it?

/// <reference types="node"/>
import Vinyl = require('vinyl');

declare function GulpPostCss(plugins?: string[], options?: GulpPostCss.Options): NodeJS.ReadWriteStream;
declare function GulpPostCss(callback?: (file: Vinyl) => { plugins?: string[], options?: GulpPostCss.Options }):
    NodeJS.ReadWriteStream;

declare namespace GulpPostCss {
    interface Options {
        parser?: Object;
    }
}

export = GulpPostCss;

Off course any or Object is the last solution, when no other solutions exists (by the way DefinitelyTyped do not accepts Object type).

3
  • Do you have to support TypeScript 2.x? If not, does the unknown type serve your needs? Commented Apr 12, 2019 at 4:50
  • @Cerberus, sorry for late answer. I think, I don't need to support TypeScript 2.x. I see, I suppose it's better than any. Commented Apr 19, 2019 at 2:25
  • I see. I'll add this as an answer, then. Commented Apr 19, 2019 at 2:33

1 Answer 1

1

In Typescript 3.0, there is a new language item called the unknown type. It is somewhat analogous to any, meaning that it could hold every value possible (everything is assignable to unknown), but with one large difference: with any you can do anything straight away, but with unknown you can't do anything or assign it to anything without checking first what value have you really got (or using an explicit type assertion, if you're sure what you've got). It is the most adequate variant to choose when there is some arbitrary mapping.

If you have to support pre-3.0 versions, you can emulate unknown type by making a union, as it's done here:

export type mixed = { [key: string]: any } | object | number | string | boolean | symbol | undefined | null | void;

declare global {
  type unknown = mixed;
}

This has a downside, however, that you can't easily include indexed types - note that the values in { [key: string]: any } there are any, not unknown, since the latter would make definition directly recursive.


Side note about Object type: I don't know any use case where it can't be safely replaced by object, except explicitly typing the constructors. That seems to be the reason it is not accepted by DefinitelyTyped. One caveat is that object type is somewhat more limiting, since you can't store, for example, a string in it (but can store the String object).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.