File: mappedTypeWithAny.js

package info (click to toggle)
node-typescript 5.0.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 459,116 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (128 lines) | stat: -rw-r--r-- 3,708 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//// [mappedTypeWithAny.ts]
type Item = { value: string };
type ItemMap<T> = { [P in keyof T]: Item };

declare let x0: keyof any;
declare let x1: { [P in any]: Item };
declare let x2: { [P in string]: Item };
declare let x3: { [P in keyof any]: Item };
declare let x4: ItemMap<any>;

// Repro from #19152

type Data = {
  value: string;
}

type StrictDataMap<T> = {
  [P in keyof T]: Data
}

declare let z: StrictDataMap<any>;
for (let id in z) {
  let data = z[id];
  let x = data.notAValue;  // Error
}

// Issue #46169.
// We want mapped types whose constraint is `keyof T` to
// map over `any` differently, depending on whether `T`
// is constrained to array and tuple types.
type Arrayish<T extends unknown[]> = { [K in keyof T]: T[K] };
type Objectish<T extends unknown> = { [K in keyof T]: T[K] };

// When a mapped type whose constraint is `keyof T` is instantiated,
// `T` may be instantiated with a `U` which is constrained to
// array and tuple types. *Ideally*, when `U` is later instantiated with `any`,
// the result should also be some sort of array; however, at the moment we don't seem
// to have an easy way to preserve that information. More than just that, it would be
// inconsistent for two instantiations of `Objectish<any>` to produce different outputs
// depending on the usage-site. As a result, `IndirectArrayish` does not act like `Arrayish`.
type IndirectArrayish<U extends unknown[]> = Objectish<U>;

function bar(arrayish: Arrayish<any>, objectish: Objectish<any>, indirectArrayish: IndirectArrayish<any>) {
    let arr: any[];
    arr = arrayish;
    arr = objectish;
    arr = indirectArrayish;
}

declare function stringifyArray<T extends readonly any[]>(arr: T): { -readonly [K in keyof T]: string };
let abc: any[] = stringifyArray(void 0 as any);

declare function stringifyPair<T extends readonly [any, any]>(arr: T): { -readonly [K in keyof T]: string };
let def: [any, any] = stringifyPair(void 0 as any);

// Repro from #46582

type Evolvable<E extends Evolver> = {
  [P in keyof E]: never;
};
type Evolver<T extends Evolvable<any> = any> = {
  [key in keyof Partial<T>]: never;
};


//// [mappedTypeWithAny.js]
"use strict";
for (var id in z) {
    var data = z[id];
    var x = data.notAValue; // Error
}
function bar(arrayish, objectish, indirectArrayish) {
    var arr;
    arr = arrayish;
    arr = objectish;
    arr = indirectArrayish;
}
var abc = stringifyArray(void 0);
var def = stringifyPair(void 0);


//// [mappedTypeWithAny.d.ts]
type Item = {
    value: string;
};
type ItemMap<T> = {
    [P in keyof T]: Item;
};
declare let x0: keyof any;
declare let x1: {
    [P in any]: Item;
};
declare let x2: {
    [P in string]: Item;
};
declare let x3: {
    [P in keyof any]: Item;
};
declare let x4: ItemMap<any>;
type Data = {
    value: string;
};
type StrictDataMap<T> = {
    [P in keyof T]: Data;
};
declare let z: StrictDataMap<any>;
type Arrayish<T extends unknown[]> = {
    [K in keyof T]: T[K];
};
type Objectish<T extends unknown> = {
    [K in keyof T]: T[K];
};
type IndirectArrayish<U extends unknown[]> = Objectish<U>;
declare function bar(arrayish: Arrayish<any>, objectish: Objectish<any>, indirectArrayish: IndirectArrayish<any>): void;
declare function stringifyArray<T extends readonly any[]>(arr: T): {
    -readonly [K in keyof T]: string;
};
declare let abc: any[];
declare function stringifyPair<T extends readonly [any, any]>(arr: T): {
    -readonly [K in keyof T]: string;
};
declare let def: [any, any];
type Evolvable<E extends Evolver> = {
    [P in keyof E]: never;
};
type Evolver<T extends Evolvable<any> = any> = {
    [key in keyof Partial<T>]: never;
};