简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM