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.

Add a Mutable type (opposite of Readonly) to lib.d.ts

See original GitHub issue

Search Terms

mutable, mutable type

Suggestion

lib.d.ts contains a Readonly type, but not a Mutable type. This type would be the opposite of Readonly, defined as:

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

Use Cases

This is useful as a lightweight builder for a type with readonly properties (especially useful when some of the properties are optional and conditional logic is needed before assignment).

Examples

 type Mutable<T> = {-readonly[P in keyof T]: T[P]};
 
 interface Foobar {
   readonly a: number;
   readonly b: number;
   readonly x?: string;
 }

 function newFoobar(baz: string): Foobar {
   const foobar: Mutable<Foobar> = {a: 1, b: 2};
   if (shouldHaveAnX(baz)) {
     foobar.x = 'someValue';
   }

   return foobar;
 }

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript / JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. new expression-level syntax)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:113
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

30reactions
baetheuscommented, Jan 17, 2020

I wish the typescript designers had gone with immutability by default instead of mutability. If all properties and variable declarations were readonly by default then I wouldn’t have to type readonly so often. Mutable properties could have used a keyword like mutable. It would be much easier to find bugs, just grep -r mutable *.ts for the likely culprits… Or, at the very least, a compiler option to default to readonly instead of mutable would be useful for many projects.

16reactions
sushruthcommented, Jul 10, 2020

Adding to this here - Mutable standard type helper would be greatly useful for apollo/graphql users.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript - Mutability and inversion of Readonly<T>
You can define Mutable such as: type Mutable<T> = { -readonly [P in keyof T]: T[P] }; . However, it's dangerous because you...
Read more >
Documentation - TypeScript 3.4
In TypeScript 3.4, the readonly modifier in a mapped type will automatically convert array-like types to their corresponding readonly counterparts. ts. // How ......
Read more >
Overview - TypeScript
If your file generally lacks semicolons, TypeScript won't add one. ... lib.d.ts type Readonly<T> = { readonly [K in keyof T]: T[K] }...
Read more >
readme.md - PN Baturaja
[`Mutable`](source/mutable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly `.
Read more >
typescript-cheatsheet - GitHub Pages
The cheatsheet contains references to types, classes, decorators, ... We will create a d.ts file that will put the jQuery $ variable in...
Read more >

github_iconTop Related Medium Post

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 Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found