Add a Mutable type (opposite of Readonly) to lib.d.ts
See original GitHub issueSearch 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:
- Created 5 years ago
- Reactions:113
- Comments:21 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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, justgrep -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.Adding to this here -
Mutable
standard type helper would be greatly useful for apollo/graphql users.