Skip to content

Commit 5dcaf53

Browse files
committed
feat: add 2857-hard-isrequiredkey
1 parent 3c65b5f commit 5dcaf53

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 1.
2+
// type IsRequiredKey<T, K extends keyof T = keyof T> = T extends {
3+
// [P in K]-?: T[P];
4+
// }
5+
// ? true
6+
// : false;
7+
8+
// 2.
9+
// type IsRequiredKey<T, K extends keyof T = keyof T> = T extends Required<
10+
// Pick<T, K>
11+
// >
12+
// ? true
13+
// : false;
14+
15+
// 3.
16+
type IsRequiredKey<T, K extends keyof T = keyof T> = T extends Record<K, T[K]>
17+
? true
18+
: false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Equal, Expect } from "@type-challenges/utils";
2+
3+
type cases = [
4+
Expect<Equal<IsRequiredKey<{ a: number; b?: string }, "a">, true>>,
5+
Expect<Equal<IsRequiredKey<{ a: number; b?: string }, "b">, false>>,
6+
Expect<Equal<IsRequiredKey<{ a: number; b?: string }, "b" | "a">, false>>
7+
];

0 commit comments

Comments
 (0)