Files
clean-architecture-frontend…/src/features/installed-feature-messages.ts
T
DongHyeonkaandClaude Opus 5.5 30cf71b249 fix(features): keep the installed composers typed with two features (09-01)
flatMap over the installed-module union, the route-registry cast and the
non-distributive locale types all failed once a second feature was installed.
A unit test installs a differently shaped second feature into a copy of src
and runs the app typecheck.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 22:10:07 +09:00

63 lines
2.4 KiB
TypeScript

import type { FeatureMessageCatalogs } from "./feature-module.ts";
import { INSTALLED_PRODUCT_FEATURE_MODULES } from "./installed-product-manifest.ts";
type InstalledModule = (typeof INSTALLED_PRODUCT_FEATURE_MODULES)[number];
type UnionToIntersection<Union> = (
Union extends unknown ? (value: Union) => void : never
) extends (value: infer Intersection) => void
? Intersection
: never;
// 09-01. These conditionals take the module as a type parameter so they
// distribute over the installed-module union. Applied to the union alias
// itself they saw only the locales every feature shares, and a second feature
// without catalogs erased the whole message table.
type ModuleCatalog<Module, Locale extends string> =
Module extends Readonly<{ messages: Readonly<Record<Locale, infer Catalog>> }>
? Catalog
: Readonly<Record<never, never>>;
type ModuleLocale<Module> = Module extends Readonly<{ messages: infer Messages }>
? Extract<keyof Messages, string>
: never;
type LocaleCatalog<Locale extends string> = UnionToIntersection<
ModuleCatalog<InstalledModule, Locale>
>;
type InstalledLocale = ModuleLocale<InstalledModule>;
/**
* Q-6.9-12. One catalog per locale that any installed feature declares. The
* locale list used to be fixed at ko-KR/en-US, so a feature's other catalogs
* were dropped without a word. A key two features define for one locale
* still throws.
*/
export function composeInstalledMessageCatalogs(
modules: readonly Readonly<{ messages: FeatureMessageCatalogs }>[],
): Readonly<Record<string, Readonly<Record<string, string>>>> {
const locales = new Map<string, Record<string, string>>();
for (const module of modules) {
for (const [locale, catalog] of Object.entries(module.messages)) {
let result = locales.get(locale);
if (!result) {
result = {};
locales.set(locale, result);
}
for (const [key, value] of Object.entries(catalog)) {
if (Object.hasOwn(result, key)) {
throw new TypeError(`Duplicate ${locale} message key: ${key}`);
}
result[key] = value;
}
}
}
return Object.freeze(
Object.fromEntries(
[...locales].map(([locale, catalog]) => [locale, Object.freeze(catalog)]),
),
);
}
export const INSTALLED_MESSAGE_CATALOGS = composeInstalledMessageCatalogs(
INSTALLED_PRODUCT_FEATURE_MODULES,
) as {
readonly [Locale in InstalledLocale]: LocaleCatalog<Locale>;
};