File: twiceNestedKeyofIndexInference.types

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (76 lines) | stat: -rw-r--r-- 2,298 bytes parent folder | download | duplicates (4)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
=== tests/cases/compiler/twiceNestedKeyofIndexInference.ts ===
type Set1<T, K1 extends keyof T> = T extends any[] ? T : Pick<T, Exclude<keyof T, K1>> & {
>Set1 : Set1<T, K1>

    [SK1 in K1]-?: Required<Pick<T, SK1>>;
}[K1];

type Set2<T, K1 extends keyof T, K2 extends keyof T[K1]> = T extends any[] ? T : Pick<T, Exclude<keyof T, K1>> & {
>Set2 : Set2<T, K1, K2>

    [SK1 in K1]-?: Required<{
        [key in K1]: Set1<T[K1], K2>;
    }>;
}[K1];

declare function set<T, K1 extends keyof T>(source: T, path: [K1], value: T[K1]): Set1<T, K1>;
>set : { <T, K1 extends keyof T>(source: T, path: [K1], value: T[K1]): Set1<T, K1>; <T, K1 extends keyof T, K2 extends keyof T[K1]>(source: T, path: [K1, K2], value: T[K1][K2]): Set2<T, K1, K2>; }
>source : T
>path : [K1]
>value : T[K1]

declare function set<T, K1 extends keyof T, K2 extends keyof T[K1]>(source: T, path: [K1, K2], value: T[K1][K2]): Set2<T, K1, K2>;
>set : { <T, K1 extends keyof T>(source: T, path: [K1], value: T[K1]): Set1<T, K1>; <T, K1 extends keyof T, K2 extends keyof T[K1]>(source: T, path: [K1, K2], value: T[K1][K2]): Set2<T, K1, K2>; }
>source : T
>path : [K1, K2]
>value : T[K1][K2]


interface State {
    a: {
>a : { b: string; c: number; }

        b: string;
>b : string

        c: number;
>c : number

    };
    d: boolean;
>d : boolean
}

const state: State = {
>state : State
>{    a: {        b: "",        c: 0,    },    d: false,} : { a: { b: string; c: number; }; d: false; }

    a: {
>a : { b: string; c: number; }
>{        b: "",        c: 0,    } : { b: string; c: number; }

        b: "",
>b : string
>"" : ""

        c: 0,
>c : number
>0 : 0

    },
    d: false,
>d : false
>false : false

};

const newState: State = set(state, ["a", 'b'], 'why'); // shouldn't be an error
>newState : State
>set(state, ["a", 'b'], 'why') : Pick<State, "d"> & Required<{ a: Pick<{ b: string; c: number; }, "c"> & Required<Pick<{ b: string; c: number; }, "b">>; }>
>set : { <T, K1 extends keyof T>(source: T, path: [K1], value: T[K1]): Set1<T, K1>; <T, K1 extends keyof T, K2 extends keyof T[K1]>(source: T, path: [K1, K2], value: T[K1][K2]): Set2<T, K1, K2>; }
>state : State
>["a", 'b'] : ["a", "b"]
>"a" : "a"
>'b' : "b"
>'why' : "why"