-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16259-medium-to-primitive.ts
47 lines (41 loc) · 1.05 KB
/
16259-medium-to-primitive.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
40
41
42
43
44
45
46
47
// ============= Test Cases =============
import type { Equal, Expect } from "./test-utils";
type PersonInfo = {
name: "Tom";
age: 30;
married: false;
addr: {
home: "123456";
phone: "13111111111";
};
hobbies: ["sing", "dance"];
readonlyArr: readonly ["test"];
fn: () => any;
};
type ExpectedResult = {
name: string;
age: number;
married: boolean;
addr: {
home: string;
phone: string;
};
hobbies: [string, string];
readonlyArr: readonly [string];
fn: Function;
};
type cases = [Expect<Equal<ToPrimitive<PersonInfo>, ExpectedResult>>];
// ============= Your Code Here =============
type Primitives = [string, number, bigint, boolean, symbol, null, undefined];
type GetPrimitive<T, P = Primitives> = P extends [infer F, ...infer R]
? T extends F
? F
: GetPrimitive<T, R>
: never;
type ToPrimitive<T> = T extends (...arg: any[]) => any
? Function
: {
[P in keyof T]: T[P] extends object
? ToPrimitive<T[P]>
: GetPrimitive<T[P]>;
};