Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created February 7, 2025 14:42
Show Gist options
  • Save Kcko/3e03301a00e98bc2930a52ca821f98ef to your computer and use it in GitHub Desktop.
Save Kcko/3e03301a00e98bc2930a52ca821f98ef to your computer and use it in GitHub Desktop.
/*
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