Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created February 7, 2025 14:42
Show Gist options
  • Save Kcko/425384dc266897a362f7b9ddcbc26082 to your computer and use it in GitHub Desktop.
Save Kcko/425384dc266897a362f7b9ddcbc26082 to your computer and use it in GitHub Desktop.
/*
Record<Keys, Type>
Keys může být string, number, symbol nebo union těchto typů.
Type může být jakýkoli typ.
Kdy Record využít
Když chcete zajistit, aby objekt měl přesnou strukturu s předem definovanými typy klíčů a hodnot.
Pokud například potřebujete typově bezpečně definovat mapování mezi hodnotami (např. slovníky nebo konfigurace).
*/
const scores: Record<string, number> = {
Alice: 10,
Bob: 20,
Charlie: 30,
};
type Person = 'name' | 'age' | 'email';
const personInfo: Record<Person, string> = {
name: 'Alice',
age: '30',
email: '[email protected]',
};
type Task = {
title: string;
completed: boolean;
};
type ProjectTasks = Record<string, Task>;
const projectStatus: ProjectTasks = {
'Task 1': { title: 'Design UI', completed: true },
'Task 2': { title: 'Implement backend', completed: false },
'Task 3': { title: 'Write tests', completed: false },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment