Template String As Object Property Name

Why are template strings not allowed as literal object keys?

Template strings are expressions, not literals1. You can only use string literals (and identifiers) for property names, for everything else – that is not known to be static – you need a computed property name.

Is it for performance reasons?

No, that’s unlikely. It’s to ease parsing, and makes it easy to distinguish constant (statically known) property names from dynamically computed ones.

And mostly, it’s a feature that no one needs. It doesn’t simplify or shorten anything, and what you would achieve with it is already possible.

Will the syntax be allowed sometime in the future?

Nope.

1: Even when they’re called “template literals”, technically they aren’t literals. And: templates don’t even need to be strings, they can evaluate to anything.

Leave a Comment