File: typeGuardsInRightOperandOfAndAndOperator.symbols

package info (click to toggle)
node-typescript 2.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 203,952 kB
  • ctags: 52,987
  • sloc: sh: 11; makefile: 5
file content (119 lines) | stat: -rw-r--r-- 5,841 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
=== tests/cases/conformance/expressions/typeGuards/typeGuardsInRightOperandOfAndAndOperator.ts ===
// In the right operand of a && operation, 
// the type of a variable or parameter is narrowed by any type guard in the left operand when true.
function foo(x: number | string) {
>foo : Symbol(foo, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 0, 0))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 2, 13))

    return typeof x === "string" && x.length === 10; // string
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 2, 13))
>x.length : Symbol(String.length, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 2, 13))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))
}
function foo2(x: number | string) {
>foo2 : Symbol(foo2, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 4, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 5, 14))

    // modify x in right hand operand
    return typeof x === "string" && ((x = 10) && x); // string | number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 5, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 5, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 5, 14))
}
function foo3(x: number | string) {
>foo3 : Symbol(foo3, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 8, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 9, 14))

    // modify x in right hand operand with string type itself
    return typeof x === "string" && ((x = "hello") && x); // string | number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 9, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 9, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 9, 14))
}
function foo4(x: number | string | boolean) {
>foo4 : Symbol(foo4, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 12, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 13, 14))

    return typeof x !== "string" // string | number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 13, 14))

        && typeof x !== "number"  // number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 13, 14))

        && x;   // boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 13, 14))
}
function foo5(x: number | string | boolean) {
>foo5 : Symbol(foo5, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 17, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 18, 14))

    // usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
    var b: number | boolean;
>b : Symbol(b, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 20, 7))

    return typeof x !== "string" // string | number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 18, 14))

        && ((b = x) && (typeof x !== "number"  // number | boolean
>b : Symbol(b, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 20, 7))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 18, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 18, 14))

        && x));   // boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 18, 14))
}
function foo6(x: number | string | boolean) {
>foo6 : Symbol(foo6, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 24, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 25, 14))

    // Mixing typeguard narrowing in if statement with conditional expression typeguard
    return typeof x !== "string" // string | number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 25, 14))

        && (typeof x !== "number" // number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 25, 14))

        ? x // boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 25, 14))

        : x === 10) // number 
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 25, 14))
}
function foo7(x: number | string | boolean) {
>foo7 : Symbol(foo7, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 31, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))

    var y: number| boolean | string;
>y : Symbol(y, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 7))

    var z: number| boolean | string;
>z : Symbol(z, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 34, 7))

    // Mixing typeguard narrowing
    return typeof x !== "string"
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))

        && ((z = x) // number | boolean
>z : Symbol(z, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 34, 7))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))

        && (typeof x === "number"
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))

        // change value of x
        ? ((x = 10) && x.toString()) // x is number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))
>x.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))

        // do not change value
        : ((y = x) && x.toString()))); // x is boolean
>y : Symbol(y, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 7))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))
>x.toString : Symbol(Object.toString, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 14))
>toString : Symbol(Object.toString, Decl(lib.d.ts, --, --))
}