File: typeGuardsWithAny.types

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 (96 lines) | stat: -rw-r--r-- 2,097 bytes parent folder | download | duplicates (5)
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
=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts ===
var x: any = { p: 0 };
>x : any
>{ p: 0 } : { p: number; }
>p : number
>0 : 0

if (x instanceof Object) {
>x instanceof Object : boolean
>x : any
>Object : ObjectConstructor

    x.p; // No error, type any unaffected by instanceof type guard
>x.p : any
>x : any
>p : any
}
else {
    x.p; // No error, type any unaffected by instanceof type guard
>x.p : any
>x : any
>p : any
}

if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : any
>"string" : "string"

    x.p; // Error, type any narrowed by primitive type check
>x.p : any
>x : string
>p : any
}
else {
    x.p; // No error, type unaffected in this branch
>x.p : any
>x : any
>p : any
}

if (typeof x === "number") {
>typeof x === "number" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : any
>"number" : "number"

    x.p; // Error, type any narrowed by primitive type check
>x.p : any
>x : number
>p : any
}
else {
    x.p; // No error, type unaffected in this branch
>x.p : any
>x : any
>p : any
}

if (typeof x === "boolean") {
>typeof x === "boolean" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : any
>"boolean" : "boolean"

    x.p; // Error, type any narrowed by primitive type check
>x.p : any
>x : boolean
>p : any
}
else {
    x.p; // No error, type unaffected in this branch
>x.p : any
>x : any
>p : any
}

if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : any
>"object" : "object"

    x.p; // No error, type any only affected by primitive type check
>x.p : any
>x : any
>p : any
}
else {
    x.p; // No error, type unaffected in this branch
>x.p : any
>x : any
>p : any
}