...

/

Keys with Constants and Symbols

Keys with Constants and Symbols

In this lesson, we will study how to use keys with constants and symbols.

We'll cover the following...

Constant

Version 2.7 brings the possibility of accessing a property using a constant and symbol. This is a minor improvement because, before 2.7, it was possible to access (read and write) a constant or symbol, but not to define a field in an interface or type.

Press + to interact
const Foo = "Foo"; // Constant value
const Bar = "Bar"; // Constant value
const Zaz = "Zaz"; // Constant value
const objectWithConstantProperties = { [Foo]: 100, [Bar]: "hello", [Zaz]: () => {} };
let a12: number = objectWithConstantProperties[Foo];
let b2334: string = objectWithConstantProperties[Bar];
console.log(a12);
console.log(b2334);

The example defined three ...