Skip to content

Commit 958d43a

Browse files
committed
Changed 34 solution and problem
1 parent d712206 commit 958d43a

File tree

2 files changed

+8
-59
lines changed

2 files changed

+8
-59
lines changed

src/06-challenges/34-internationalization.problem.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { expect, it } from "vitest";
2-
import { Equal, Expect } from "../helpers/type-utils";
3-
4-
// HELPERS
52

63
type GetParamKeys<TTranslation extends string> = TTranslation extends ""
74
? []
@@ -12,18 +9,6 @@ type GetParamKeys<TTranslation extends string> = TTranslation extends ""
129
type GetParamKeysAsUnion<TTranslation extends string> =
1310
GetParamKeys<TTranslation>[number];
1411

15-
// CODE
16-
17-
/**
18-
* In this example, the inner bits of the functions have been built out.
19-
* Feel free to modify them if needed, but the solution uses this code.
20-
*
21-
* Your job is to try and make the solutions type-safe.
22-
*/
23-
const makeTranslations = (translations: unknown) => {
24-
return translations;
25-
};
26-
2712
const translate = (translations: unknown, key: unknown, ...args: unknown[]) => {
2813
const translation = translations[key];
2914
const params: any = args[0] || {};
@@ -33,24 +18,11 @@ const translate = (translations: unknown, key: unknown, ...args: unknown[]) => {
3318

3419
// TESTS
3520

36-
const translations = makeTranslations({
21+
const translations = {
3722
title: "Hello, {name}!",
3823
subtitle: "You have {count} unread messages.",
3924
button: "Click me!",
40-
});
41-
42-
type tests = [
43-
Expect<
44-
Equal<
45-
typeof translations,
46-
{
47-
title: "Hello, {name}!";
48-
subtitle: "You have {count} unread messages.";
49-
button: "Click me!";
50-
}
51-
>
52-
>,
53-
];
25+
} as const;
5426

5527
it("Should translate a translation without parameters", () => {
5628
const buttonText = translate(translations, "button");

src/06-challenges/34-internationalization.solution.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { F } from "ts-toolbelt";
21
import { expect, it } from "vitest";
3-
import { Equal, Expect } from "../helpers/type-utils";
4-
5-
// HELPERS
62

73
type GetParamKeys<TTranslation extends string> = TTranslation extends ""
84
? []
@@ -13,22 +9,16 @@ type GetParamKeys<TTranslation extends string> = TTranslation extends ""
139
type GetParamKeysAsUnion<TTranslation extends string> =
1410
GetParamKeys<TTranslation>[number];
1511

16-
// CODE
17-
18-
const makeTranslations = <TTranslations extends Record<string, string>>(
19-
translations: F.Narrow<TTranslations>,
20-
) => {
21-
return translations;
22-
};
23-
2412
const translate = <
2513
TTranslations extends Record<string, string>,
2614
TKey extends keyof TTranslations,
27-
TDynamicParams = GetParamKeysAsUnion<TTranslations[TKey]>,
15+
TDynamicKeys = GetParamKeysAsUnion<TTranslations[TKey]>
2816
>(
2917
translations: TTranslations,
3018
key: TKey,
31-
...args: TDynamicParams extends string ? [Record<TDynamicParams, string>] : []
19+
...args: TDynamicKeys extends string
20+
? [dynamicArgs: Record<TDynamicKeys, string>]
21+
: []
3222
) => {
3323
const translation = translations[key];
3424
const params: any = args[0] || {};
@@ -38,24 +28,11 @@ const translate = <
3828

3929
// TESTS
4030

41-
const translations = makeTranslations({
31+
const translations = {
4232
title: "Hello, {name}!",
4333
subtitle: "You have {count} unread messages.",
4434
button: "Click me!",
45-
});
46-
47-
type tests = [
48-
Expect<
49-
Equal<
50-
typeof translations,
51-
{
52-
title: "Hello, {name}!";
53-
subtitle: "You have {count} unread messages.";
54-
button: "Click me!";
55-
}
56-
>
57-
>,
58-
];
35+
} as const;
5936

6037
it("Should translate a translation without parameters", () => {
6138
const buttonText = translate(translations, "button");

0 commit comments

Comments
 (0)