-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathnarrowingDestructuring.js
89 lines (83 loc) · 2.39 KB
/
narrowingDestructuring.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//// [narrowingDestructuring.ts]
type X = { kind: "a", a: string } | { kind: "b", b: string }
function func<T extends X>(value: T) {
if (value.kind === "a") {
value.a;
const { a } = value;
} else {
value.b;
const { b } = value;
}
}
type Z = { kind: "f", f: { a: number, b: string, c: number } }
| { kind: "g", g: { a: string, b: number, c: string }};
function func2<T extends Z>(value: T) {
if (value.kind === "f") {
const { f: f1 } = value;
const { f: { a, ...spread } } = value;
value.f;
} else {
const { g: { c, ...spread } } = value;
value.g;
}
}
function func3<T extends { kind: "a", a: string } | { kind: "b", b: number }>(t: T) {
if (t.kind === "a") {
const { kind, ...r1 } = t;
const r2 = (({ kind, ...rest }) => rest)(t);
}
}
function farr<T extends [number, string, string] | [string, number, number]>(x: T) {
const [head, ...tail] = x;
if (x[0] === 'number') {
const [head, ...tail] = x;
}
}
//// [narrowingDestructuring.js]
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
function func(value) {
if (value.kind === "a") {
value.a;
var a = value.a;
}
else {
value.b;
var b = value.b;
}
}
function func2(value) {
if (value.kind === "f") {
var f1 = value.f;
var _a = value.f, a = _a.a, spread = __rest(_a, ["a"]);
value.f;
}
else {
var _b = value.g, c = _b.c, spread = __rest(_b, ["c"]);
value.g;
}
}
function func3(t) {
if (t.kind === "a") {
var kind = t.kind, r1 = __rest(t, ["kind"]);
var r2 = (function (_a) {
var kind = _a.kind, rest = __rest(_a, ["kind"]);
return rest;
})(t);
}
}
function farr(x) {
var head = x[0], tail = x.slice(1);
if (x[0] === 'number') {
var head_1 = x[0], tail_1 = x.slice(1);
}
}