TypeScript: Support `readonly` type operator to allow correct definition of `Object.freeze`
TypeScript Version: 2.0@RC
Code
// Current definition of `Object.freeze`:
// freeze<T>(o: T): T;
const o = Object.freeze({a: 1});
o.a = 2; // Should throw type error
console.log(o); // {a: 1}
Expected behavior:
Frozen object has readonly
properties
Actual behavior:
Type system allows me to write to o
’s properties
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 53
- Comments: 46 (17 by maintainers)
@deregtd what about
Would like to add my voice to the request for a deep, recursive readonly. Would be super useful and make immutability very good in TypeScript.
In our huge Typescript app, we’re using a data storage mechanism that keeps complicated class-based types inside data stores. The rest of the app consumes these objects from the stores, but there’s always a risk of developers trying to modify the objects that come out of the stores, which ruins delta checking across the app. We could use object.freeze(), but that plays havoc with JIT.
Instead, we maintain duplicate deep readonly versions of all of our model objects. The stores distribute readonly versions, and if anyone actually needs to update them, they first clone the readonly object back into an impl object, modify it, and then shove it back in the store. The store takes it in and stores it as a readonly version.
The general data access pattern works fantastically. There’s no javascript overhead from object.freeze(), and our engineers are unable to do terrible things with our data models (outside of <any> casting, of course…) The big issue is just that we have a ton of these deep readonly models that we’re manually maintaining, and also having to have comments to maintain which methods are readonly-accessible. We’re used to just using “const” in C++ to solve this problem, both on the accessors (from stores) and for the readonly-safe methods in a class, so this is definitely a step backward, and has introduced some engineer errors by not keeping the two versions in sync.
At least we have readonly at all, it was a huge boon to us to get that. 😃 I’m just hoping for a true deep “const”-style readonly someday.
I can currently use this:
Which allows me to set each property as readonly:
The issue I have with this is
type Foo
isn’t named, and I lose other benefits of using an interface over a type def.I would like to be able to do something like this:
It could be a deep readonly, but I would be ok with it being shallow. Ultimately I’m trying to avoid doing this:
If someone adds another property, it may not be clear that
Foo
is supposed to be treated as an immutable type.There is no readonly type operator; i.e.
freeze<T>(o: T): readonly T;
. so there is no way to do this at the time being.We have discussed adding a readonly type operator that would recursively mark all properties as readonly.
I propose the issue be closed now. Read-only array types have been around for a year, which means the following works as expected. @DanielRosenwasser @RyanCavanaugh
I just want to submit that ideally a solution here could also solve another problem which is one of my biggest gripes with TypeScript: unnecessary type widening. Since
readonly
fields should not be widened, it seems like literals passed toObject.freeze
should not have their types widened:In the linked issue I proposed a
readonly
term operator for this purpose, but it would actually be preferable if it were possible to express a type forObject.freeze
that could accomplish this, since then I could give the same type to a function which did nothing but return the original object, eliminating the runtime overhead if all I want is the static check. Thus being able to typeObject.freeze
such that it inferred the type{ readonly x: 3; readonly y: 'hello' }
in the above example would be a strictly more powerful feature and would kill two birds with one stone.Here’s a deep readonly that also makes Arrays deeply readonly: Playground.
Let me know if you find any issues with it. Thanks to @tycho01 for some of the utility types.
@asfernandes neither for tuples. Without mapped conditional types is pretty hard to write conditional logic. This is the best result I got so far (with some bad hacks)
Not sure why function props are singled out in #21316. Perhaps to showcase the usage of
NonFunctionPropertyNames<T>
? Regardless, unless I’m missing something, this ought to do the trick:Playground.
Note the union in
DeepReadonlyObject<T>
, it takes care of both regular functions and hybrid interfaces-functions, like this one:Just in case, I don’t know TS that well, this may have unexpected side effects.
One more go: I think I’ve ironed out a lot of problems. It now works for primitives, for ES6 Map and Set, and for some pretty complex nested structures. Best of all, all you need to do is export the “Const” type and use it, and type T can be assigned to type Const<T> without issue.
I started with some of the ideas from @gcanti and also used the “TupleHasIndex” trick from @tycho01’s typical repo. This code only works for me under Typescript 2.5.2 with target ES6.
I’ll wrap this up in a library and make it available.
Interesting. Will have to play with that concept. That still doesn’t get us there (can’t have immutable methods and mutable methods, so anything with methods is hosed), but it might help some of our simpler method-less models that we currently have copies of (RO and RW). Thanks!
See also:
One note:
DeepReadOnly
implementation in https://github.com/Microsoft/TypeScript/pull/21316 removes all function properties from provided object, butObject.freeze
doesn’t.@AlexGalays: the following we can’t preserve yet, limitations not of his type but of our set of operators we can use today:
Note that at #12424 they’re similarly trying to lift a key based operator to become ‘deep’, in their case partial/? rather than readonly.
Very nice, @gcanti! I think I was able to extend your example to be closer to working!
Note that things will still break if I make Foo a DeepReadonly object. However, with this method, we can at least use DeepReadonly as a replacement for C++ style “const” on functions, since that use case only ever depends on one level of DeepReadonly being available. That’s the main way I wanted to use it, so I’m very pleased. Nice trick with the “_type” / “_kind” way to go from Array<T> -> T!
@gcanti this does not work correctly for embedded arrays.