Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc: How to document a type that is either Object or null?

If I have a parameter of type Object or null, should I document it as {Object} or {?Object} or {Object|null}?

I read https://jsdoc.app/tags-type.html and it says {?number} means either a number or null, but I'm not sure if I need this for objects, or just primitives.

like image 981
Will Kanga Avatar asked Oct 20 '25 10:10

Will Kanga


1 Answers

In javascript, you can use the optional chaining like element?.addEventListener() to check for null or not. So because of this, it's more intuitive for {?Object}. Just make sure that it is actually accepting null and not undefined. They have separate meanings.

like image 60
moredrowsy Avatar answered Oct 22 '25 00:10

moredrowsy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!