Skip to content

Latest commit

 

History

History

writable

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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 = "new.email@gmail.com";
  //   ^^^^^
};

const updateUser = (user: User) => {
  user.id = "random-id";
  user.email = "new.email@gmail.com";
};

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