Utility Typ | Použití na | Akce (Whitelist/Blacklist) | Popis | Podobné Funkce |
---|---|---|---|---|
Pick |
Objekty | Whitelist | Vybere určité klíče z objektu a vytvoří nový typ. | Extract |
Omit |
Objekty | Blacklist | Odstraní specifikované klíče z objektu a vytvoří nový typ. | Exclude |
Partial |
Objekty | - | Změní všechny vlastnosti objektu na volitelné. | - |
Required |
Objekty | - | Změní všechny vlastnosti objektu na povinné. | - |
Readonly |
Objekty | - | Změní všechny vlastnosti objektu na pouze pro čtení. | - |
`Record |
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
/* | |
Omit je utility typ v TypeScriptu, který umožňuje vytvořit nový typ s vynechanými (omitted) klíči z původního typu. To znamená, že nový typ bude obsahovat všechny vlastnosti z původního typu kromě těch, | |
které explicitně specifikujete k vynechání | |
Omit<T, K> | |
*/ | |
// 1 | |
interface User { | |
id: number; |
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
/* | |
Exclude<T, K> | |
Exclude je utility typ v TypeScriptu, který umožňuje vytvořit nový typ odstraněním určitých hodnot z existujícího typu. Tento typ pracuje především s tzv. union typy (sjednocení). Jeho syntaxe vypadá takto: | |
*/ | |
// 1 | |
type Status = 'active' | 'inactive' | 'pending' | 'deleted'; | |
type VisibleStatus = Exclude<Status, 'deleted'>; |
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
/* | |
Extract je utility typ v TypeScriptu, který umožňuje vytvořit nový typ extrahováním (vybráním) hodnot, které jsou společné mezi dvěma typy. Funguje jako opak Exclude, protože místo odstranění hodnot vybírá ty, které se shodují. Syntaxe vypadá takto: | |
Extract<T, U> | |
*/ | |
// 1 | |
type Status = 'active' | 'inactive' | 'pending' | 'deleted'; | |
type ActiveStatus = Extract<Status, 'active' | 'pending'>; |
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
/* | |
NonNullable je utility typ v TypeScriptu, který odstraní null a undefined z daného typu. To znamená, že výsledný typ bude obsahovat všechny hodnoty z původního typu kromě null a undefined | |
NonNullable<T> | |
*/ | |
// 1 | |
type UserInput = string | number | null | undefined; | |
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
// union | |
type StringOrNumber = string | number; | |
// intersection | |
type User = { id: number }; | |
type Admin = { isAdmin: boolean }; | |
type AdminUser = User & Admin; // { id: number; isAdmin: boolean; } | |
// return type | |
type GetUserType = () => { id: number; name: string }; |
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
// Casting = přetypování v TS nelze | |
// Asserce je explicitni určený typu | |
let x: unknown; | |
x = []; | |
// 1 | |
let result = (x as number[]).push(111); |
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
/* | |
omezení, která můžeme přidat na generické typy, aby splňovaly určité podmínky. | |
*/ | |
// T musí být objekt | |
function getProperty<T extends object, K extends keyof T>(obj: T, key: K): T[K] { | |
return obj[key]; | |
} | |
// Použití: |
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
// 1. Primitivní typy - lze zjednodušit | |
// S generics: | |
function processValue<T extends string>(value: T) { } | |
// Jednodušeji: | |
function processValue(value: string) { } | |
// 2. Union typy - lze zjednodušit | |
type Fruit = "apple" | "banana" | "orange"; | |
// S generics: | |
function processFruit<T extends Fruit>(fruit: T) { } |
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
type Pozdrav = `ahoj${string}`; | |
let ok: Pozdrav = "ahojsvete"; // ✅ | |
let nok: Pozdrav = "nazdar"; // ❌ musí začínat "ahoj" | |
// -------------------- // | |
type Velikost = "small" | "medium" | "large"; | |
type Barva = "red" | "blue"; | |
type TrickoSVelikosti = `${Velikost}-${Barva}`; |