TypeScript provides several utility types that makes working with types easier. Today I want to take a look at the Readonly<T> type.
The Readonly<T>
type makes all the properties in T
become read-only, it does so by annotating each field with the readonly
keyword while mapping:
It is useful to make an immutable version of a type. Here is an example:
If I load this code in the TypeScript playground, I get the following compiler errors:
I was hoping that this TypeScript code in someway was transpiled to an Object.Freeze() implementation but this turns out not to be the case. The TypeScript Readonly<T>
type provides only static typing immutability(in favor of performance) whereas Object.Freeze()
provides runtime immutability.
It is however possible to combine the two: