Created
February 7, 2025 14:36
-
-
Save Kcko/071d84921b96851dbd9ba1d2547dbe64 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
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}`; | |
let tricko: TrickoSVelikosti = "small-red"; // ✅ | |
let tricko2: TrickoSVelikosti = "small-green"; // ❌ green není v typu Barva | |
// -------------------- // | |
// Uppercase/Lowercase - převod na velká/malá písmena | |
type Pozdrav = "ahoj" | "nazdar"; | |
type KriciciPozdrav = `${Uppercase<Pozdrav>}!`; // typ je "AHOJ!" | "NAZDAR!" | |
// Capitalize - první písmeno velké | |
type Capitalize<S extends string> = `${Uppercase<S[0]>}${Lowercase<S>}`; | |
type Jmeno = Capitalize<"petr">; // typ je "Petr" | |
// -------------------- // | |
// string s přesnou délkou 2 | |
type DvaPismena = string & { length: 2 }; | |
type Status = `${DvaPismena}-${number}`; | |
let ok: Status = "OK-123"; // ✅ | |
let nok: Status = "OK1-123"; // ❌ první část musí mít délku 2 | |
// -------------------- // | |
type Email = `${string}@${string}.${string}`; | |
let email: Email = "[email protected]"; // ✅ | |
let neplatny: Email = "testexample.com"; // ❌ chybí @ | |
// -------------------- // | |
// Definujeme povolené hex znaky | |
type HexDigit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | |
| "a" | "b" | "c" | "d" | "e" | "f"; | |
// Přesná definice hex barvy | |
type HexColor = `#${HexDigit}${HexDigit}${HexDigit}${HexDigit}${HexDigit}${HexDigit}`; | |
let barva1: HexColor = "#ff00ff"; // ✅ | |
let barva2: HexColor = "#ff00"; // ❌ příliš krátké | |
let barva3: HexColor = "#gg0000"; // ❌ 'g' není hex digit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment