Immutability
JavaScript is a language with a few ways to declare that
some of your objects don't change. The most prominent is
const - which says that the value won't change.
// You cannot change helloWorld now, TypeScript will give
you an error about this, because you would get one at
runtime instead.
const helloWorld = "Hello World";
// Why care about immutability? A lot of this is about
reducing complexity in your code. If you can reduce the
number of things which can change, then there are less
things to keep track of.
Using const is a great first step, however this fails
down a bit when using objects.
helloWorld = "Hi world";
// myConstantObject is not quite a constant though, because
we can still make changes to parts of the object, for
example we can change msg:
const myConstantObject = {
msg: "Hello World",
};
// const means the value at that point stays the same, but
that the object itself may change internally. This can
be changed using Object.freeze.
myConstantObject.msg = "Hi World";
// When an object is frozen, then you cannot change the
internals. TypeScript will offer errors in these cases:
const myDefinitelyConstantObject = Object.freeze({
msg: "Hello World",
});
// Using freeze means you can trust that the object is
staying the same under the hood.
TypeScript has a few extra syntax hooks to improve working
with immutable data which you can find in the TypeScript
section of the examples:
example:literals
example:type-widening-and-narrowing
myDefinitelyConstantObject.msg = "Hi World";
// This works the same for arrays too:
const myFrozenArray = Object.freeze(["Hi"]);
myFrozenArray.push("World");