💻 Tech
To declare an object with immutable properties, you can use const assertion. This can be used as constants values across your app.
export const icons = { icon1: 'icon1', icon2: 'icon2', icon3: 'icon3' } as const;
With this, you can safely use icons.icon1
without the risk of its value being changed in some parts of the code. Without as const
, the value of properties like icon1
can be changed.
For example, the code below will result to an error.
const icons = { icon1: 'icon1', icon2: 'icon2', icon3: 'icon3' } as const;
icons.icon1 = ""; // Cannot assign to 'icon1' because it is a read-only property.
Another example, the code below will not result to an error because the properties of icons
are mutable.
const icons = { icon1: 'icon1', icon2: 'icon2', icon3: 'icon3' };
icons.icon1 = "";