Created
May 30, 2024 15:46
-
-
Save ReVoid/a9e0b468641fa53a6ff1973cdbc91344 to your computer and use it in GitHub Desktop.
Text dictionary with optional keys
This file contains hidden or 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
// If you need to create a text dictionary with not all of it keys | |
type TermDictionary<TKey extends string> = Partial<Record<TKey, string>>; | |
type StatusUnion = 'Success' | 'Error' | 'Unknown'; | |
enum StatusEnum { | |
Success, | |
Error, | |
Unknown, | |
}; | |
const messagesByUnion: TermDictionary<StatusUnion> = { | |
Success: 'Ok', | |
Error: 'Not Ok', | |
// Unknown: 'Something went wrong', // ✅ Not every single key is required | |
} | |
// Enums are runtime objects, so you have to extract their type with 'keyof' + 'typeof' operators | |
const messagesByEnm: TermDictionary<keyof typeof StatusEnum> = { | |
Success: 'Ok', | |
Error: 'Not Ok', | |
// Unknown: 'Something went wrong', // ✅ Not every single key is required | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment