Pick<T, Keys>
Pick<T, Keys> là utility type trả về object type chỉ chứa các key được chỉ định trong Keys từ kiểu T.
Type argument của Pick<T, Keys>
T
Type argument T nhận object type.
Keys
Keys chỉ định property key của object type T. Nếu chỉ định property key không tồn tại trong object type T, sẽ gây compile error.
Ví dụ sử dụng Pick
tstypeUser = {surname : string;middleName ?: string;givenName : string;age : number;address ?: string;nationality : string;createdAt : string;updatedAt : string;};typePerson =Pick <User , "surname" | "middleName" | "givenName">;
tstypeUser = {surname : string;middleName ?: string;givenName : string;age : number;address ?: string;nationality : string;createdAt : string;updatedAt : string;};typePerson =Pick <User , "surname" | "middleName" | "givenName">;
Person ở trên sẽ giống với kiểu sau:
tstypePerson = {surname : string;middleName ?: string;givenName : string;};
tstypePerson = {surname : string;middleName ?: string;givenName : string;};
Ví dụ về việc theo dõi thay đổi kiểu với Pick
Giả sử bạn tạo dịch vụ xử lý sách và object Book biểu diễn sách được định nghĩa như sau:
tstypeBook = {id : number;title : string;author : string;createdAt :Date ;updatedAt :Date ;};
tstypeBook = {id : number;title : string;author : string;createdAt :Date ;updatedAt :Date ;};
Tham khảo điều này, giả sử bạn tạo BookInputData làm input data để tạo Book. Nó được tạo từ request bên ngoài, và id, createdAt, updatedAt được gán sau đó bởi dịch vụ này, thì BookInputData sẽ như sau:
tstypeBookInputData = {title : string;author : string;};
tstypeBookInputData = {title : string;author : string;};
Giả sử property author cần phải là Person thay vì string. Nếu định nghĩa Book, BookInputData độc lập, bạn phải thay đổi property author của mỗi cái khi có thay đổi này.
tstypeBook = {id : number;title : string;author :Person ; // 変更箇所createdAt :Date ;updatedAt :Date ;};typeBookInputData = {title : string;author :Person ; // 変更箇所};
tstypeBook = {id : number;title : string;author :Person ; // 変更箇所createdAt :Date ;updatedAt :Date ;};typeBookInputData = {title : string;author :Person ; // 変更箇所};
Nếu các định nghĩa này gần nhau thì còn tốt, nhưng nếu ở các file khác nhau sẽ rất khó tìm.
Do đó, định nghĩa lại BookInputData sử dụng Pick<T, K>:
tstypeBookInputData =Pick <Book , "title" | "author">;
tstypeBookInputData =Pick <Book , "title" | "author">;
Với cách này, BookInputData ít nhất có liên kết với Book trong code, và sẽ tự động theo dõi thay đổi kiểu của property author.
Thông tin liên quan
📄️ Omit<T, Keys>
Tạo object type loại bỏ các property tùy ý