Experiment: Making TypeScript Immutable-by-Default
4 days ago
- #Programming
- #TypeScript
- #Immutability
- The author explores making TypeScript immutable by default, similar to Rust's approach.
- They attempt to replace TypeScript's built-in libraries with immutable versions by setting 'noLib' in TSConfig.
- A minimal standard library is created to type-check basic operations without built-in types.
- Immutable arrays are successfully implemented by defining read-only properties and methods.
- Mutable arrays are introduced as an opt-in feature using a 'MutableArray' type.
- The concept is extended to 'Record' types, making them immutable by default with a mutable variant.
- The author fails to enforce immutability on plain objects without annotations.
- The experiment concludes with a call for solutions to make plain objects immutable by default in TypeScript.