Created
February 7, 2025 14:42
-
-
Save Kcko/3e03301a00e98bc2930a52ca821f98ef 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
/* | |
Readonly je utility typ v TypeScriptu, | |
který změní všechny vlastnosti daného typu na pouze pro čtení (readonly) | |
Readonly<T> | |
*/ | |
// 1 | |
interface User { | |
id: number; | |
name: string; | |
email: string; | |
} | |
const user: Readonly<User> = { | |
id: 1, | |
name: 'Alice', | |
email: '[email protected]', | |
}; | |
// Pokus o změnu vlastnosti způsobí chybu | |
user.name = 'Bob'; // Error: Cannot assign to 'name' because it is a read-only property. | |
// 2 | |
interface Config { | |
apiEndpoint: string; | |
timeout: number; | |
} | |
const config: Readonly<Config> = { | |
apiEndpoint: 'https://api.example.com', | |
timeout: 3000, | |
}; | |
// Tento pokus o úpravu způsobí chybu: | |
config.timeout = 5000; // Error: Cannot assign to 'timeout' because it is a read-only property. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment