1

I have seen Google using a lot of Type Expressions in various APIs. Can someone explain to me what good they do? Are they only there to highlight/colorcode a certain functions, or ease readability? Or do they serve some actual purpose?

I'm a bit confused as they're used as together with commented-out code but differ from regular comments with /** @type */ instead of /* @type */ (which do not color-code the comment)

Could someone give me the how's and why's of Type Expressions?

enter image description here

5
  • Could you show examples of what you're talking about? Commented Jul 31, 2017 at 17:32
  • Sure, hold on just a sec. Commented Jul 31, 2017 at 17:32
  • 2
    It is used for type coercing. You can read more here github.com/google/closure-compiler/wiki/Type-Expressions Commented Jul 31, 2017 at 17:33
  • What is type coercing? Commented Jul 31, 2017 at 17:34
  • 2
    The capacity of the compiler to force you to write something that is coherent with the type definitions. It would warn you for instance if you try to call push on window object. Commented Jul 31, 2017 at 17:35

1 Answer 1

2

You will see these in a lot of places other than Google. They can be used for Closure Compiler to improve optimization and also provide a bit of type safety by warning when there are type errors.

But more generally these are JSDoc tags, which allow you to annotate and describe javascript code. The tags can document what types of data you code expects to receive and what will be returned. This can then be used to automatically produce documentation about your code and can also be used by text editors and IDEs to give feedback while working. Many popular editors support this such as Sublime Text and Visual Studio.

Lots of information here: http://usejsdoc.org/index.html

Sign up to request clarification or add additional context in comments.

1 Comment

That's really interesting. Had zero clue about this. Many thanks Mark!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.