Skip to content

Instantly share code, notes, and snippets.

@ReVoid
Created May 30, 2024 15:46
Show Gist options
  • Save ReVoid/a9e0b468641fa53a6ff1973cdbc91344 to your computer and use it in GitHub Desktop.
Save ReVoid/a9e0b468641fa53a6ff1973cdbc91344 to your computer and use it in GitHub Desktop.
Text dictionary with optional keys
// 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