
"I like programming languages where variables are immutable by default. For example, in Rust, let declares an immutable variable and let mut declares a mutable one. I've long wanted this in other languages, like TypeScript, which is mutable by default-the opposite of what I want! I wondered: is it possible to make TypeScript values immutable by default? My goal was to do this purely with TypeScript, without changing TypeScript itself."
"Step 1: obliterate the built-in libraries TypeScript has built-in type definitions for JavaScript APIs like Array and Date and String. If you've ever changed the or options in your TSConfig, you've tweaked which of these definitions are included. For example, you might add the "ES2024" library if you're targeting a newer runtime. My goal was to swap the built-in libraries with an immutable-by-default replacement. The first step was to stop using any of the built-in libraries. I set the flag in my TSConfig, like this:"
An experiment attempted to make TypeScript values immutable by default using only TypeScript's type system and no external tools. The experiment removed the default lib definitions via tsconfig to replace the built-in types with immutable-by-default alternatives. After removing the libs, the compiler reported missing global types such as Array, String, and Boolean, confirming the replacement step. Progress was made converting arrays and Records to be immutable by default. Regular object types could not be fully converted, producing a partial failure that highlights limitations in TypeScript's current type system.
Read at Evanhahn
Unable to calculate read time
Collection
[
|
...
]