6 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
The article explores an experiment to enforce immutability in TypeScript without external tools. The author succeeded with arrays and records but struggled with regular objects, ultimately seeking help from the community to find a complete solution.
If you do, here's more
The article explores an experiment to make TypeScript immutable by default, a concept inspired by languages like Rust. The author aims to achieve this without altering TypeScript itself or relying on external tools, focusing instead on modifying TypeScript's built-in libraries. By disabling the built-in libraries in the TypeScript configuration and creating a minimal custom library, he successfully begins the process of implementing immutability for arrays and the Record type.
In the first significant step, the author creates an immutable array type by defining a readonly property accessor in the custom library. This allows accessing elements without modifying them and prevents any mutation operations. He tests this with various operations and confirms that non-mutating methods work while mutation attempts fail. He also introduces a mutable array type, which requires explicit declaration, maintaining immutability as the default.
Attempting to extend this approach to the Record type, he successfully defines both immutable and mutable versions of it. However, his attempt to apply the same principle to regular objects proves unsuccessful. Despite various attempts at altering the Object type to achieve immutability, he encounters limitations. The article ends with an open challenge for readers to find a solution for making plain objects immutable by default.
Questions about this article
No questions yet.