[Hi there — I recently published a free book on TypeScript, Yet Another TypeScript Book, that you can read here: https://www.gitbook.com/book/pagalvin/yet-another-typescript-book/details.]

The fact that the open/closed paren — () — is an operator in JavaScript and not just compiler bird seed finally hit me the other week. That plus string interpolation in TypeScript led to this little postlet.

TypeScript supports something called string interpolation. This is apparently called template literals in ECMA 6. String interpolation is quite useful and I strongly recommend that you take advantage of it if you’re writing TypeScript today or can use ECMA 6 in your projects. Here’s a good write-up if you’re interested: https://basarat.gitbooks.io/typescript/content/docs/template-strings.html

I was writing some code when the () function invoke operator lightbulb went off. I dropped what I was doing and immediately thought to nail down the concept by adding a log message to a TS/Angular 1.x app I’m working on:

Greetings, Earth Man

$log just wraps around console.log with some posh Angular stuff. These are the really neat bits:

Using string interpolation with the open and closed backtick

Defining a lambda function that that returns the string “Paul” (that’s me!)

Invoking that lambda function immediately using the operator form of ().

Again, the key insight for me was that () is an operator, no different from +, -, etc. Cool stuff.

If you’re interested, this currently transpiles down to:

I SAID, “Greetings, Earth man”

I’ve been using named and lambda functions left and right for a long time now, but this insight escaped me until just recently. Hopefully this is useful to someone.

Happy coding.

</end>