for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
import { localizedField, localizedFieldNonNull } from "../models/app";
export type Locales = "en" | "fr";
type TranslatableKeysNonNull<T> = {
[K in keyof T]: T[K] extends localizedFieldNonNull ? K : never;
}[keyof T];
type TranslatableKeys<T> = {
[K in keyof T]: T[K] extends localizedField ? K : never;
export function localizeField<T>(
locale: Locales,
model: T,
field: TranslatableKeys<T>,
): string | null {
const a = model[field];
return model[field][locale];
}
export function localizeFieldNonNull<T>(
field: TranslatableKeysNonNull<T>,
): string {
export function getLocale(locale: string): Locales {
if (locale === "en" || locale === "fr") {
return locale;
console.log("Warning: unknown locale. Defaulting to en.");
return "en";