Skip to content

Commit 0c9d41d

Browse files
committed
feat: add 0270-hard-typed-get
1 parent ae6e1b7 commit 0c9d41d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 参考 https://github.com/type-challenges/type-challenges/issues/9654
2+
type Get<
3+
T extends Record<string, any>,
4+
K extends string
5+
> = K extends `${infer Key}.${infer Path}`
6+
? Get<T[Key], Path>
7+
: K extends keyof T
8+
? T[K]
9+
: never;
10+
11+
// K extends keyof T 返回值
12+
// type cases = keyof {
13+
// foo: {
14+
// bar: {
15+
// value: "foobar";
16+
// count: 6;
17+
// };
18+
// included: true;
19+
// };
20+
// hello: "world";
21+
// };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Equal, Expect } from "@type-challenges/utils";
2+
3+
type cases = [
4+
Expect<Equal<Get<Data, "hello">, "world">>,
5+
Expect<Equal<Get<Data, "foo.bar.count">, 6>>,
6+
Expect<Equal<Get<Data, "foo.bar">, { value: "foobar"; count: 6 }>>,
7+
8+
Expect<Equal<Get<Data, "no.existed">, never>>
9+
];
10+
11+
type Data = {
12+
foo: {
13+
bar: {
14+
value: "foobar";
15+
count: 6;
16+
};
17+
included: true;
18+
};
19+
hello: "world";
20+
};

0 commit comments

Comments
 (0)