Is there a way to define generic types in JS documentation?

11,259

Interestingly, Google Closure Compiler has support for generic types, with a syntax like this:

/**
 * @constructor
 * @template T
 */
Foo = function() { ... };

/** @return {T} */
Foo.prototype.get = function() { ... };

/** @param {T} t */
Foo.prototype.set = function(t) { ... };

/** @type {!Foo.<string>} */ var foo = new Foo();
var foo = /** @type {!Foo.<string>} */ (new Foo());

As JSDuck already supports Closure Compiler style type annotations, it should be already possible to write types like {MyClass.<T>}. JSDuck doesn't however uses a @template tag for a different purpose altogether, but one implement its own custom tag like @typevar or override the builtin @template to do your bidding using the custom tags system.

But as there is no actual generic types support in JSDuck, it won't check your generic types. On the contrary, it will probably complain, that you're referencing an unknown type T and others. But it's easy to make JSDuck ignore certain types (or type variables) by using --external=T.

One final note. The Closure Compiler doesn't support your extends syntax in type variables, and I don't really see why would you write T extends Bird and then {MyArray<T>}, instead of just writing {MyArray<Bird>}.

Share:
11,259
Egor Nepomnyaschih
Author by

Egor Nepomnyaschih

Updated on July 13, 2022

Comments

  • Egor Nepomnyaschih
    Egor Nepomnyaschih almost 2 years

    I'm looking for documentation generator for my JS library. I find JSDuck the most comprehensive and powerful one. But I don't see a way to define type variables for generic classes and functions using its syntax. Quick look at popular JS documentation generators makes me feel that neither of them has a capability of doing that. Here is a pseudo-example of what I'm looking for:

    /**
     * @class MyArray
     * My perfect array class.
     * @typevar T
     */
    MyArray = function() ...
    
    /**
     * @class BirdArray
     * Please count birds using this awesome array class.
     * @typevar T extends {Bird}
     * @extends {MyArray<T>}
     */
    BirdArray = function() ...
    extend(BirdArray, MyArray);
    

    Sample output:

    MyArray<T>
    My perfect array class.

    BirdArray<T extends Bird> extends MyArray<T>
    Please count birds using this awesome array class.

    Is there a way of achieving that in JSDuck? If not, is there some JS documentation generator which can do that for me? Please assume that it should be as versatile as JSDuck, to make sure that I'll be able to use arbitrary class inheritance pattern.

  • Egor Nepomnyaschih
    Egor Nepomnyaschih almost 11 years
    Thank you for information. I won't go with custom tags, because generic types logic is pretty complicated in all programming languages. It can be a big project. Your suggestion about --external=T is very useful.
  • Egor Nepomnyaschih
    Egor Nepomnyaschih almost 11 years
    The reason to write BirdArray<T extends Bird> is that you can define explicit BirdArray<Duck> in this implementation instead of just abstract BirdArray.