[Typescript] 45. Easy - PickValue

export type PickValue<T extends object, K = keyof T> = K extends keyof T ? T[K] : never;

interface Person {
 name: string;
 address: {
   postcode: string;
   street: string;  
 }
}

const p: Person = {
  name: 'Jon',
  address: {
    postcode: '123',
    street: 'abc'  
  }  
}

/*
{
  postcode: string;
  street: string;
}
*/
type addressType = PickValue<Person, 'address'> 
type nameType = PickValue<Person, 'name'>
type notExistType = PickValue<Person, 'notExists'>

 

posted @ 2022-10-13 00:00  Zhentiw  阅读(16)  评论(0编辑  收藏  举报