how to extend a type in jsdoc 1

how to extend a type in jsdoc

 /**
 * @typedef {Object} ChildType
 * @property {String} childProp
 *
 * @typedef {Base & ChildType} Child
 */

Here is what the above code is Doing:
1. We’re defining a base type called `Base` with a property called `baseProp`.
2. We’re defining a child type called `Child` that extends `Base` and adds a property called `childProp`.
3. We’re defining a type called `ChildType` that is an alias for `Child`.
4. We’re defining a type called `ChildType` that is an alias for `Child`.

Now, let’s see how we can use these types in our code:

“`js
/**
* @param {ChildType} child
*/
function doSomething(child) {
console.log(child.baseProp);
console.log(child.childProp);
}

doSomething({
baseProp: ‘base’,
childProp: ‘child’
});
“`

Here, we’re defining a function called `doSomething` that takes a parameter of type `ChildType`.

We’re then calling `doSomething` with an object that has the properties `baseProp` and `childProp`.

This is valid because `ChildType` is an alias for `Child`, which is a type that extends `Base` and adds a property called `childProp`.

## Conclusion

In this article, we looked at how to use JSDoc to define types and type aliases.

We also looked at how to use type aliases to extend types.

To learn more about JSDoc, check out the [JSDoc documentation](http://usejsdoc.org/).

Similar Posts