I've a type used in a class
type MyType {
haha: number,
optional?: string
}
class MyClass{
constructor(private config: MyType) {
}
}
How to document MyType to explain what haha and optional are used for?
I would use and interface for MyType and put JSDoc comments on the properties. Also, have you tried an @param jsdoc comment on the constructor?
/** Description of MyType interface */
interface MyType {
/** This property is ... */
haha: number,
/** Optional property useful for ... */
optional?: string
}
/**
* Description of what MyClass is
*/
class MyClass{
/**
* @param config Docs on config property.It is different than the interface docs
*/
constructor(private config: MyType) {
}
}
Thanks @aciccarello ... Would you please explain me what's the difference between a type and an interface?
I believe types were introduced later for some specific use cases like type aliases. This SO questions has a good answer describing the differences. Generally I use interfaces for most cases and then use a type if I want an alias, such as getting the type of a specific interface property.
Interface types have many similarities to type aliases for object type literals, but since interface types offer more capabilities they are generally preferred to type aliases.
_StackOverflow:_ Typescript: Interfaces vs Types
Thank you :) @aciccarello
Anyone know if you can document a type like the original question asks? I have lots of types, and I'm interested in documenting them
That still doesn't really have an answer. So it should be an issue because there are no docs for type properties.
It's possible to document an interface, but inline docs for properties won't work for types. The only option, for now, is to duplicate it with jsdoc like.
/**
* @property foo Some docs.
*/
type MyFooType = {
/** that comment is ignored */
foo:? string;
}
Most helpful comment
I would use and interface for MyType and put JSDoc comments on the properties. Also, have you tried an
@paramjsdoc comment on the constructor?