jsdoc/jsdoc.github.io

Why the question mark for type tag?

markcellus opened this issue · 1 comments

Sorry if this is the wrong repository to bring this up. If so, just let me know and I will move as needed.

A coworker and I were having a discussion about JSDocs and the @type tag. And I'm curious of why JSDoc allows a ? symbol to denote a nullable type. Why {?number} when {null|number} is available, which is more readable and explicit?

I originally thought it may have been because it's commonly understood in certain programming languages that ? intuitively means null but, after doing a bit of research, there are only a couple of programming languages that use ? to denote a "null"-like value (swift) and I can't think of any place where JavaScript does this.

It's because JSDoc supports Closure Compiler's syntax for type expressions, and that's what the Closure Compiler folks picked.

You can write {(null|number)} or {null|number} if you prefer, though.