File: keyRemappingKeyofResult.js

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 (99 lines) | stat: -rw-r--r-- 2,553 bytes parent folder | download | duplicates (3)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//// [keyRemappingKeyofResult.ts]
const sym = Symbol("")
type Orig = { [k: string]: any, str: any, [sym]: any }

type Okay = Exclude<keyof Orig, never>
// type Okay = string | number | typeof sym

type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any }
/* type Remapped = {
    str: any;
    [sym]: any;
} */
// no string index signature, right?

type Oops = Exclude<keyof Remapped, never>
declare let x: Oops;
x = sym;
x = "str";
// type Oops = typeof sym <-- what happened to "str"?

// equivalently, with an unresolved generic (no `exclude` shenanigans, since conditions won't execute):
function f<T>() {
    type Orig = { [k: string]: any, str: any, [sym]: any } & T;
    
    type Okay = keyof Orig;
    let a: Okay;
    a = "str";
    a = sym;
    a = "whatever";
    // type Okay = string | number | typeof sym
    
    type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any }
    /* type Remapped = {
        str: any;
        [sym]: any;
    } */
    // no string index signature, right?
    
    type Oops = keyof Remapped;
    let x: Oops;
    x = sym;
    x = "str";
}

// and another generic case with a _distributive_ mapping, to trigger a different branch in `getIndexType`
function g<T>() {
    type Orig = { [k: string]: any, str: any, [sym]: any } & T;
    
    type Okay = keyof Orig;
    let a: Okay;
    a = "str";
    a = sym;
    a = "whatever";
    // type Okay = string | number | typeof sym

    type NonIndex<T extends PropertyKey> = {} extends Record<T, any> ? never : T;
    type DistributiveNonIndex<T extends PropertyKey> = T extends unknown ? NonIndex<T> : never;
    
    type Remapped = { [K in keyof Orig as DistributiveNonIndex<K>]: any }
    /* type Remapped = {
        str: any;
        [sym]: any;
    } */
    // no string index signature, right?
    
    type Oops = keyof Remapped;
    let x: Oops;
    x = sym;
    x = "str";
}

export {};

//// [keyRemappingKeyofResult.js]
const sym = Symbol("");
x = sym;
x = "str";
// type Oops = typeof sym <-- what happened to "str"?
// equivalently, with an unresolved generic (no `exclude` shenanigans, since conditions won't execute):
function f() {
    let a;
    a = "str";
    a = sym;
    a = "whatever";
    let x;
    x = sym;
    x = "str";
}
// and another generic case with a _distributive_ mapping, to trigger a different branch in `getIndexType`
function g() {
    let a;
    a = "str";
    a = sym;
    a = "whatever";
    let x;
    x = sym;
    x = "str";
}
export {};