Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 797
Egor Nepomnyaschih Avatar asked Sep 11 '13 09:09

Egor Nepomnyaschih


People also ask

How do I restrict a generic type in Java?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

What is the correct way to define a generic class in TypeScript?

TypeScript supports generic classes. The generic type parameter is specified in angle brackets after the name of the class. A generic class can have generic fields (member variables) or methods. In the above example, we created a generic class named KeyValuePair with a type variable in the angle brackets <T, U> .

How do you declare a generic type in a class explain?

The declaration of a generic class is almost the same as that of a non-generic class except the class name is followed by a type parameter section. The type parameter section of a generic class can have one or more type parameters separated by commas.

How do you pass a generic class as parameter in TypeScript?

Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.


1 Answers

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>}.

like image 83
Rene Saarsoo Avatar answered Oct 01 '22 01:10

Rene Saarsoo