Created
May 6, 2020 20:11
-
-
Save dscheerens/29fe42555a75fc626b6f6791d677dbe7 to your computer and use it in GitHub Desktop.
Utility function to define type safe translation 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
const TRANSLATION_KEY_PLACEHOLDER = Symbol('TRANSLATION_KEY_PLACEHOLDER'); | |
type TranslationKeyPlaceholder = typeof TRANSLATION_KEY_PLACEHOLDER; | |
export interface TranslationStructure { | |
[key: string]: TranslationStructure | TranslationKeyPlaceholder; | |
} | |
export type TranslationKeysOf<T extends TranslationStructure> = { | |
[P in keyof T]: T[P] extends TranslationStructure ? TranslationKeysOf<T[P]> : string | |
}; | |
export function defineTranslationKeys<T extends TranslationStructure>( | |
translationStructure: (t: TranslationKeyPlaceholder) => T | |
): TranslationKeysOf<T> { | |
return generateTranslationKeys(translationStructure(TRANSLATION_KEY_PLACEHOLDER)); | |
} | |
function generateTranslationKeys<U extends TranslationStructure>(structure: U, prefix?: string): TranslationKeysOf<U> { | |
return Object.keys(structure).reduce<Partial<TranslationKeysOf<U>>>( | |
(keys, key) => { | |
const value = structure[key]; | |
const path = prefix === undefined ? key : `${prefix}.${key}`; | |
return { | |
...keys, | |
[key]: value === TRANSLATION_KEY_PLACEHOLDER ? path : generateTranslationKeys(value, path) | |
}; | |
}, | |
{} | |
) as TranslationKeysOf<U>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment