Skip to content

Latest commit

 

History

History
 
 

readonly-keys

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

ReadonlyKeys<Type> constructs a union type by picking all readonly properties of object type Type

interface UserThatCannotChangeEmailAndName {
  id: number;
  readonly email: string;
  readonly name: string;
}

type PropertiesThatUserCannotChange = ReadonlyKeys<UserThatCannotChangeEmailAndName>;
//   ^? 'email' | 'name'

It means properties which values cannot be reassigned

declare const user: UserThatCannotChangeEmailAndName;

// error: Cannot assign to 'email' because it is a read-only property
user.email = "[email protected]";
//   ^^^^^
// error: Cannot assign to 'name' because it is a read-only property
user.name = "Alex";
//   ^^^^

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