Skip to content

Latest commit

 

History

History

mark-writable

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

MarkWritable<Type, Keys> constructs a type by picking all properties from type Type where properties Keys remove readonly modifier, meaning they can be reassigned

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

type UserThatCanChangeEmail = MarkWritable<ReadonlyUser, "email">;
//   ^? { readonly id: string; email: string }

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

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

const updateUserEmail = (user: UserThatCanChangeEmail) => {
  user.email = "[email protected]";
};

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