Consider the following scenario:
const common = {
"test": "Test",
"test2": "Test2"
}
const greetings = {
"hello": "Hello"
}
export const locales = (["en", "nl"] as const);
export type I18nMap = Record<typeof locales[number], {
common: typeof common;
greetings: typeof greetings;
}>;;
I am looking to be able to choose a single key like "common"
or an array of keys such as
["common", ""greetings"
. Depending on the selected key(s), I want to have the ability to select any of the keys within the chosen object. I'm unsure how to accomplish this, below is my attempt
type Namespace = keyof I18nMap[keyof I18nMap];
export function useTranslation<
T extends Namespace | Namespace[],
U extends T extends Namespace
? keyof I18nMap[keyof I18nMap][Namespace]
: keyof I18nMap[keyof I18nMap][Namespace][number]
>(namespace: T) {}
For example, when the namespace is "common", I anticipate only "test" | "test2"
to be available for type U. If I select an array ["common", "greetings"], I expect a union of "test" | "test2" | "hello"
for type U