Created
February 7, 2025 14:42
-
-
Save Kcko/425384dc266897a362f7b9ddcbc26082 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
/* | |
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