Skip to content

Commit 70da1bc

Browse files
committed
add i18n compiletime checks
1 parent 5efaf0c commit 70da1bc

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/lib/i18n.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ type NonFunctionMessageKey<FrMessages extends Record<string, string | ((params:
8686
type FunctionMessageKey<FrMessages extends Record<string, string | ((params: any) => string)>> =
8787
Exclude<keyof FrMessages, NonFunctionMessageKey<FrMessages>>;
8888

89-
type X = NonFunctionMessageKey<{ foo: string; bar: (params: { x: number }) => string }>;
90-
type Y = FunctionMessageKey<{ foo: string; bar: (params: { x: number }) => string }>;
91-
9289
export function createComponentI18nApi<
9390
ComponentName extends string,
9491
FrMessages extends Record<string, string | ((params: any) => string)>

test/types/i18n.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createComponentI18nApi } from "../../src/lib/i18n";
2+
import { assert } from "tsafe/assert";
3+
import type { Equals } from "tsafe";
4+
5+
const { useTranslation, addSomeDsfrComponentTranslations } = createComponentI18nApi({
6+
"componentName": "SomeDsfrComponent",
7+
"frMessages": {
8+
/* spell-checker: disable */
9+
10+
"do something": ({ what }: { what: string }) => `Faire ${what}`,
11+
"the good": "le bien"
12+
/* spell-checker: enable */
13+
}
14+
});
15+
16+
addSomeDsfrComponentTranslations({
17+
"lang": "en",
18+
"messages": {
19+
"do something": ({ what }) => {
20+
assert<Equals<typeof what, string>>();
21+
return `Do ${what}`;
22+
},
23+
"the good": "the good"
24+
}
25+
});
26+
27+
const { t } = useTranslation();
28+
29+
t("do something", { "what": t("the good") });

0 commit comments

Comments
 (0)