Skip to content

Commit ab90c00

Browse files
committed
Added another exercise
1 parent 4ac1144 commit ab90c00

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Equal, Expect } from "../helpers/type-utils";
2+
3+
const getValue = <TObj>(obj: TObj, key: keyof TObj) => {
4+
return obj[key];
5+
};
6+
7+
const obj = {
8+
a: 1,
9+
b: "some-string",
10+
c: true,
11+
};
12+
13+
const numberResult = getValue(obj, "a");
14+
const stringResult = getValue(obj, "b");
15+
const booleanResult = getValue(obj, "c");
16+
17+
type tests = [
18+
Expect<Equal<typeof numberResult, number>>,
19+
Expect<Equal<typeof stringResult, string>>,
20+
Expect<Equal<typeof booleanResult, boolean>>,
21+
];
22+
23+
export {};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Equal, Expect } from "../helpers/type-utils";
2+
3+
const getValue = <TObj, TKey extends keyof TObj>(obj: TObj, key: TKey) => {
4+
return obj[key];
5+
};
6+
7+
const obj = {
8+
a: 1,
9+
b: "some-string",
10+
c: true,
11+
};
12+
13+
const numberResult = getValue(obj, "a");
14+
const stringResult = getValue(obj, "b");
15+
const booleanResult = getValue(obj, "c");
16+
17+
type tests = [
18+
Expect<Equal<typeof numberResult, number>>,
19+
Expect<Equal<typeof stringResult, string>>,
20+
Expect<Equal<typeof booleanResult, boolean>>,
21+
];
22+
23+
export {};

0 commit comments

Comments
 (0)