-
Notifications
You must be signed in to change notification settings - Fork 0
/
05181-hard-mutable-keys.ts
39 lines (35 loc) · 1010 Bytes
/
05181-hard-mutable-keys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// ============= Test Cases =============
import type { Equal, Expect } from "./test-utils";
type cases = [
Expect<Equal<MutableKeys<{ a: number; readonly b: string }>, "a">>,
Expect<Equal<MutableKeys<{ a: undefined; readonly b: undefined }>, "a">>,
Expect<
Equal<
MutableKeys<{ a: undefined; readonly b?: undefined; c: string; d: null }>,
"a" | "c" | "d"
>
>,
Expect<Equal<MutableKeys<{}>, never>>
];
// ============= Your Code Here =============
type MutableKeys<T> = keyof {
[Key in keyof T as Equal<Pick<T, Key>, Readonly<Pick<T, Key>>> extends true
? never
: Key]: Key;
};
type MutableKeys2<T> = keyof {
-readonly [Key in keyof T as Equal<
Pick<T, Key>,
Readonly<Pick<T, Key>>
> extends true
? never
: Key]: Key;
};
type test1 = MutableKeys<{ a: number; readonly b: string }>;
type test2 = MutableKeys<{ a: undefined; readonly b: undefined }>;
type test3 = MutableKeys<{
a: undefined;
readonly b?: undefined;
c: string;
d: null;
}>;