Skip to content

Commit 4bbaba8

Browse files
authored
Merge pull request total-typescript#23 from total-typescript/matt/improved-problem-solution-for-34
Improved problem/solution for 34
2 parents d81c0b4 + a78b7c2 commit 4bbaba8

File tree

5 files changed

+29
-33
lines changed

5 files changed

+29
-33
lines changed

package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"cross-fetch": "^3.1.5",
1212
"jsdom": "^21.1.1",
1313
"prettier": "^2.8.7",
14-
"typescript": "^5.0.3",
14+
"typescript": "^5.1.6",
1515
"vite-tsconfig-paths": "^4.0.7",
1616
"vitest": "^0.31.1"
1717
},
@@ -107,4 +107,4 @@
107107
"dependencies": {
108108
"ts-toolbelt": "^9.6.0"
109109
}
110-
}
110+
}

scripts/tests/__snapshots__/all.test.ts.snap

+14-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ src/02-passing-type-arguments/07-create-new-set.problem.ts(12,10): error TS2344:
2121
src/02-passing-type-arguments/07-create-new-set.problem.ts(13,10): error TS2344: Type 'false' does not satisfy the constraint 'true'.
2222
src/02-passing-type-arguments/08-default-generics.problem.ts(14,10): error TS2344: Type 'false' does not satisfy the constraint 'true'.
2323
src/02-passing-type-arguments/09-generics-in-type-arguments-in-arguments.problem.ts(15,24): error TS18046: 'component' is of type 'unknown'.
24-
src/02-passing-type-arguments/09-generics-in-type-arguments-in-arguments.problem.ts(28,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
2524
src/02-passing-type-arguments/10-reduce.problem.ts(14,3): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
2625
No index signature with a parameter of type 'string' was found on type '{}'.
2726
src/02-passing-type-arguments/10-reduce.problem.ts(28,24): error TS2344: Type 'false' does not satisfy the constraint 'true'.
@@ -66,11 +65,8 @@ src/04-generics-advanced/20.5-spotting-missing-generics.problem.ts(20,10): error
6665
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(31,17): error TS7006: Parameter 'source' implicitly has an 'any' type.
6766
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(33,25): error TS7006: Parameter 'source' implicitly has an 'any' type.
6867
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(34,24): error TS7006: Parameter 'source' implicitly has an 'any' type.
69-
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(38,10): error TS2344: Type 'false' does not satisfy the constraint 'true'.
7068
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(38,33): error TS2339: Property 'getFullName' does not exist on type '{}'.
71-
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(40,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
7269
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(40,28): error TS2339: Property 'getFirstAndLastName' does not exist on type '{}'.
73-
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(43,5): error TS2344: Type 'false' does not satisfy the constraint 'true'.
7470
src/04-generics-advanced/20.7-working-around-partial-inference.problem.ts(43,28): error TS2339: Property 'getFirstNameLength' does not exist on type '{}'.
7571
src/05-function-overloads/21-what-is-a-function-overload.problem.ts(13,22): error TS2344: Type 'false' does not satisfy the constraint 'true'.
7672
src/05-function-overloads/21-what-is-a-function-overload.problem.ts(13,52): error TS2344: Type 'false' does not satisfy the constraint 'true'.
@@ -125,11 +121,11 @@ src/06-challenges/30-form-validator.problem.ts(83,5): error TS2578: Unused '@ts-
125121
src/06-challenges/32-data-fetcher-with-warning.problem.ts(22,12): error TS2344: Type 'false' does not satisfy the constraint 'true'.
126122
src/06-challenges/33-compose.problem.ts(21,24): error TS2344: Type 'false' does not satisfy the constraint 'true'.
127123
src/06-challenges/33-compose.problem.ts(28,5): error TS2578: Unused '@ts-expect-error' directive.
128-
src/06-challenges/34-internationalization.problem.ts(13,23): error TS18046: 'translations' is of type 'unknown'.
129-
src/06-challenges/34-internationalization.problem.ts(16,43): error TS7006: Parameter '_' implicitly has an 'any' type.
130-
src/06-challenges/34-internationalization.problem.ts(16,46): error TS7006: Parameter 'key' implicitly has an 'any' type.
131-
src/06-challenges/34-internationalization.problem.ts(42,3): error TS2578: Unused '@ts-expect-error' directive.
132-
src/06-challenges/34-internationalization.problem.ts(47,3): error TS2578: Unused '@ts-expect-error' directive.
124+
src/06-challenges/34-internationalization.problem.ts(10,23): error TS18046: 'translations' is of type 'unknown'.
125+
src/06-challenges/34-internationalization.problem.ts(13,43): error TS7006: Parameter '_' implicitly has an 'any' type.
126+
src/06-challenges/34-internationalization.problem.ts(13,46): error TS7006: Parameter 'key' implicitly has an 'any' type.
127+
src/06-challenges/34-internationalization.problem.ts(39,3): error TS2578: Unused '@ts-expect-error' directive.
128+
src/06-challenges/34-internationalization.problem.ts(44,3): error TS2578: Unused '@ts-expect-error' directive.
133129
,"
134130
`;
135131

@@ -1877,9 +1873,15 @@ exports[`vitest > Should have the correct Vitest errors 1`] = `
18771873
"ancestorTitles": [
18781874
"",
18791875
],
1880-
"failureMessages": [],
1876+
"failureMessages": [
1877+
"expected 'You have 123123 unread messages.' to deeply equal 'You have 2 unread messages.'",
1878+
],
18811879
"fullName": " Should translate a translation WITH parameters",
1882-
"status": "passed",
1880+
"location": {
1881+
"column": 20,
1882+
"line": 45,
1883+
},
1884+
"status": "failed",
18831885
"title": "Should translate a translation WITH parameters",
18841886
},
18851887
{
@@ -1903,7 +1905,7 @@ exports[`vitest > Should have the correct Vitest errors 1`] = `
19031905
],
19041906
"message": "",
19051907
"name": "src/06-challenges/34-internationalization.solution.ts",
1906-
"status": "passed",
1908+
"status": "failed",
19071909
},
19081910
],
19091911
}

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

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ type GetParamKeys<TTranslation extends string> = TTranslation extends ""
66
? [Param, ...GetParamKeys<Tail>]
77
: [];
88

9-
type GetParamKeysAsUnion<TTranslation extends string> =
10-
GetParamKeys<TTranslation>[number];
11-
129
const translate = (translations: unknown, key: unknown, ...args: unknown[]) => {
1310
const translation = translations[key];
1411
const params: any = args[0] || {};

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ type GetParamKeys<TTranslation extends string> = TTranslation extends ""
66
? [Param, ...GetParamKeys<Tail>]
77
: [];
88

9-
type GetParamKeysAsUnion<TTranslation extends string> =
10-
GetParamKeys<TTranslation>[number];
11-
129
const translate = <
1310
TTranslations extends Record<string, string>,
1411
TKey extends keyof TTranslations,
15-
TDynamicKeys = GetParamKeysAsUnion<TTranslations[TKey]>
12+
TParamKeys extends string[] = GetParamKeys<TTranslations[TKey]>,
1613
>(
1714
translations: TTranslations,
1815
key: TKey,
19-
...args: TDynamicKeys extends string
20-
? [dynamicArgs: Record<TDynamicKeys, string>]
21-
: []
16+
...args: TParamKeys extends []
17+
? []
18+
: [params: Record<TParamKeys[number], string>]
2219
) => {
2320
const translation = translations[key];
2421
const params: any = args[0] || {};
@@ -42,7 +39,7 @@ it("Should translate a translation without parameters", () => {
4239

4340
it("Should translate a translation WITH parameters", () => {
4441
const subtitle = translate(translations, "subtitle", {
45-
count: "2",
42+
count: "123123",
4643
});
4744

4845
expect(subtitle).toEqual("You have 2 unread messages.");

0 commit comments

Comments
 (0)