Matthew Layton
Matthew Layton

Reputation: 42390

TypeScript - Mutability and inversion of Readonly<T>

Assume that I have the following mutable class:

class Foo {
    constructor(public bar: any) { }
}

I can define readonly instances of this class like so:

const foo: Readonly<Foo> = new Foo(123);
foo.bar = 456; // error, can't reassign to bar because it's readonly.

What I'd like to be able to do is the inverse of this, where the class is immutable:

class Foo {
    constructor(public readonly bar: any) { }
}

And then be able to make mutable versions like so:

const foo: Mutable<Foo> = new Foo(123);
foo.bar = 456;

Is this possible?

Upvotes: 13

Views: 5800

Answers (2)

Kael Kirk
Kael Kirk

Reputation: 352

Slight modification to the accepted answer that can get you past the nested readonly definitions:

type Mutable<T> = {
  -readonly [P in keyof T]: Mutable<T[P]>;
};

Upvotes: 0

satanTime
satanTime

Reputation: 13584

Yes, you can use -readonly in type definition.

type Mutable<T> = {
  -readonly [P in keyof T]: T[P];
};

const foo: Mutable<Foo> = new Foo(123);
foo.bar = 456;

Playground

But remember it's only type definition, it doesn't change original logic.

type Mutable<T> = {
  -readonly [P in keyof T]: T[P];
};

class Foo {
    get test(): boolean {
      return true;
    }

    constructor(public readonly bar: any) { }
}

const foo: Mutable<Foo> = new Foo(123);
foo.bar = 456;
foo.test = false; // oops, it will cause an error.

Upvotes: 8

Related Questions