forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 2
/
narrowingAssignmentReadonlyRespectsAssertion.js
71 lines (63 loc) · 1.9 KB
/
narrowingAssignmentReadonlyRespectsAssertion.js
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
//// [narrowingAssignmentReadonlyRespectsAssertion.ts]
// https://github.com/microsoft/TypeScript/issues/41984
interface TestCase<T extends string | number> {
readonly val1: T | ReadonlyArray<T>;
readonly val2: ReadonlyArray<T>;
}
interface MultiCaseFixture<T> {
cases: T[];
}
function subDataFunc(): TestCase<string | number>[] {
return [
{ val1: "a", val2: ["a", "b", "c"] },
{ val1: 2, val2: [1, 2, 3] },
{ val1: ["a", "z"], val2: ["x", "y", "z"] },
{ val1: [5, 10], val2: [10, 100, 1000] },
];
}
function dataFunc<T>(subFunc: () => T[]): MultiCaseFixture<T> {
return { cases: subFunc() };
}
function testFunc() {
const fixture = dataFunc<TestCase<string | number>>(subDataFunc);
fixture.cases.forEach(({ val1, val2 }) => {
if (Array.isArray(val1)) {
// This should retain val1 as being an array
const reversedVal1 = val1.slice().reverse();
console.log(reversedVal1);
} else {
console.log(val1);
}
console.log(val2);
});
}
testFunc();
//// [narrowingAssignmentReadonlyRespectsAssertion.js]
// https://github.com/microsoft/TypeScript/issues/41984
function subDataFunc() {
return [
{ val1: "a", val2: ["a", "b", "c"] },
{ val1: 2, val2: [1, 2, 3] },
{ val1: ["a", "z"], val2: ["x", "y", "z"] },
{ val1: [5, 10], val2: [10, 100, 1000] },
];
}
function dataFunc(subFunc) {
return { cases: subFunc() };
}
function testFunc() {
var fixture = dataFunc(subDataFunc);
fixture.cases.forEach(function (_a) {
var val1 = _a.val1, val2 = _a.val2;
if (Array.isArray(val1)) {
// This should retain val1 as being an array
var reversedVal1 = val1.slice().reverse();
console.log(reversedVal1);
}
else {
console.log(val1);
}
console.log(val2);
});
}
testFunc();