Created
February 7, 2025 14:42
-
-
Save Kcko/3be1ff38ac048c31e0562b9709168f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Pick: | |
Pick<T, K> je utility typ v TypeScriptu, který vytváří nový typ výběrem určitých vlastností K z typu T. | |
Používá se pro vytvoření podmnožiny existujícího typu. | |
*/ | |
// 1 | |
interface User { | |
id: number; | |
name: string; | |
email: string; | |
phone: number; | |
} | |
type UserInfo = Pick<User, 'id' | 'name'>; | |
type UserBasicInfo = Pick<User, 'name' | 'email'>; | |
const user: UserInfo = { | |
id: 1, | |
name: 'abc', | |
}; | |
const userDetails: UserBasicInfo = { | |
name: 'abc', | |
email: '[email protected]', | |
}; | |
interface TestedPerson { | |
name: string; | |
age: number; | |
address: string; | |
email: string; | |
phone: string; | |
} | |
// 2 Kombinace Pick s jinými utility typy | |
type OptionalPersonBasicInfo = Partial<Pick<TestedPerson, 'name' | 'age'>>; | |
const partialBasicInfo: OptionalPersonBasicInfo = { | |
name: 'Bob', | |
// age může být vynecháno | |
}; | |
// 3 Použití Pick s vnořenými objekty | |
interface ComplexPerson { | |
name: string; | |
age: number; | |
address: { | |
street: string; | |
city: string; | |
country: string; | |
}; | |
} | |
type PersonNameAndCity = Pick<ComplexPerson, 'name'> & Pick<ComplexPerson['address'], 'city'>; | |
const nameAndCity: PersonNameAndCity = { | |
name: 'Charlie', | |
city: 'New York', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment