Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 745 Bytes

README.md

File metadata and controls

31 lines (25 loc) · 745 Bytes

Writable<Type> constructs a type with removed readonly for all properties of Type

interface ReadonlyUser {
  readonly id: string;
  readonly email: string;
}

type User = Writable<ReadonlyUser>;
//   ^? { id: string; email: string }

It means the properties of the constructed type can be reassigned:

const cannotUpdateUser = (user: ReadonlyUser) => {
  // Cannot assign to 'id' because it is a read-only property
  user.id = "random-id";
  //   ^^
  // Cannot assign to 'email' because it is a read-only property
  user.email = "[email protected]";
  //   ^^^^^
};

const updateUser = (user: User) => {
  user.id = "random-id";
  user.email = "[email protected]";
};

TS Playground – https://tsplay.dev/mxjqBw