question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: DeepReadonly<T> type

See original GitHub issue

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)

Code

It would be nice to have a shard, standard library type that allows to express deep readonly-ness (not really const, since methods are out of scope, but still…):

interface Y { a: number; }
interface X { y: Y; }
let x: Readonly<X> = {y: {a: 1}};
x.y.a = 2;  // Succeeds, which is expected, but it'd be nice to have a common way to express deep readonly

type DeepReadonly<T> = {
  readonly [P in keyof T]: DeepReadonly<T[P]>;
}
let deepX: DeepReadonly<X> = {y: {a: 1}};
deepX.y.a = 2; // Fails as expected!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:240
  • Comments:51 (7 by maintainers)

github_iconTop GitHub Comments

42reactions
andyflemingcommented, Oct 8, 2019

With how complex these DeepReadOnly definitions are, it would be nice to see it provided directly by TypeScript.

42reactions
nieltgcommented, Jul 6, 2018

This is my implementation of DeepReadonly. I named it Immutable so it doesn’t clash with Readonly.

type Primitive = undefined | null | boolean | string | number | Function

type Immutable<T> =
  T extends Primitive ? T :
    T extends Array<infer U> ? ReadonlyArray<U> :
      T extends Map<infer K, infer V> ? ReadonlyMap<K, V> : Readonly<T>

type DeepImmutable<T> =
  T extends Primitive ? T :
    T extends Array<infer U> ? DeepImmutableArray<U> :
      T extends Map<infer K, infer V> ? DeepImmutableMap<K, V> : DeepImmutableObject<T>

interface DeepImmutableArray<T> extends ReadonlyArray<DeepImmutable<T>> {}
interface DeepImmutableMap<K, V> extends ReadonlyMap<DeepImmutable<K>, DeepImmutable<V>> {}
type DeepImmutableObject<T> = {
  readonly [K in keyof T]: DeepImmutable<T[K]>
}

It handles ReadonlyArray and ReadonlyMap. It also handles Function types so their instances still can be called after being applied by this modifier.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DeepReadonly Object Typescript - Stack Overflow
I modified the example slightly to use the type inference for the readonly array value type because I find (infer R)[] clearer than...
Read more >
Are readonly interfaces possible? : r/typescript - Reddit
type DeepReadonly<T> = { readonly [Key in keyof T]: DeepReadonly<T[Key]>; } ... I would suggest against using readonly on anything but arrays.
Read more >
`DeepReadonly` doesn't handle arrays properly - Issuehunt
Suggested solution(s). I was able to have to rule removed by changing T extends _DeepReadonlyArray<infer U> (https://github.com/piotrwitek/utility-types/blob ...
Read more >
Microsoft/TypeScript - Gitter
where().NonBannedUsers();. and i know that i cannot use class generic type in static method, but this shows the idea. I tried numerous times...
Read more >
Deep Readonly
Implement a generic DeepReadonly<T> which makes every parameter of an object and its ... type DeepReadonly<T> = { readonly [P in keyof T]:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found