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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
//// [privateNameInInExpression.ts]
class Foo {
#field = 1;
static #staticField = 2;
#method() {}
static #staticMethod() {}
goodRhs(v: any) {
const a = #field in v;
const b = #field in v.p1.p2;
const c = #field in (v as {});
const d = #field in (v as Foo);
const e = #field in (v as never);
for (let f in #field in v as any) { /**/ } // unlikely but valid
}
badRhs(v: any) {
const a = #field in (v as unknown); // Bad - RHS of in must be object type or any
const b = #fiel in v; // Bad - typo in privateID
const c = (#field) in v; // Bad - privateID is not an expression on its own
for (#field in v) { /**/ } // Bad - 'in' not allowed
for (let d in #field in v) { /**/ } // Bad - rhs of in should be a object/any
}
whitespace(v: any) {
const a = v && /*0*/#field/*1*/
/*2*/in/*3*/
/*4*/v/*5*/
}
flow(u: unknown, n: never, fb: Foo | Bar, fs: FooSub, b: Bar, fsb: FooSub | Bar, fsfb: Foo | FooSub | Bar) {
if (typeof u === 'object') {
if (#field in n) {
n; // good n is never
}
if (#field in u) {
u; // good u is Foo
} else {
u; // good u is object | null
}
if (u !== null) {
if (#field in u) {
u; // good u is Foo
} else {
u; // good u is object
}
if (#method in u) {
u; // good u is Foo
}
if (#staticField in u) {
u; // good u is typeof Foo
}
if (#staticMethod in u) {
u; // good u is typeof Foo
}
}
}
if (#field in fb) {
fb; // good fb is Foo
} else {
fb; // good fb is Bar
}
if (#field in fs) {
fs; // good fs is FooSub
} else {
fs; // good fs is never
}
if (#field in b) {
b; // good b is 'Bar & Foo'
} else {
b; // good b is Bar
}
if (#field in fsb) {
fsb; // good fsb is FooSub
} else {
fsb; // good fsb is Bar
}
if (#field in fsfb) {
fsfb; // good fsfb is 'Foo | FooSub'
} else {
fsfb; // good fsfb is Bar
}
class Nested {
m(v: any) {
if (#field in v) {
v; // good v is Foo
}
}
}
}
}
class FooSub extends Foo { subTypeOfFoo = true }
class Bar { notFoo = true }
function badSyntax(v: Foo) {
return #field in v; // Bad - outside of class
}
//// [privateNameInInExpression.js]
"use strict";
class Foo {
#field = 1;
static #staticField = 2;
#method() { }
static #staticMethod() { }
goodRhs(v) {
const a = #field in v;
const b = #field in v.p1.p2;
const c = #field in v;
const d = #field in v;
const e = #field in v;
for (let f in #field in v) { /**/ } // unlikely but valid
}
badRhs(v) {
const a = #field in v; // Bad - RHS of in must be object type or any
const b = #fiel in v; // Bad - typo in privateID
const c = (#field) in v; // Bad - privateID is not an expression on its own
for (#field in v) { /**/ } // Bad - 'in' not allowed
for (let d in #field in v) { /**/ } // Bad - rhs of in should be a object/any
}
whitespace(v) {
const a = v && /*0*/ #field /*1*/
/*2*/ in /*3*/
/*4*/ v; /*5*/
}
flow(u, n, fb, fs, b, fsb, fsfb) {
if (typeof u === 'object') {
if (#field in n) {
n; // good n is never
}
if (#field in u) {
u; // good u is Foo
}
else {
u; // good u is object | null
}
if (u !== null) {
if (#field in u) {
u; // good u is Foo
}
else {
u; // good u is object
}
if (#method in u) {
u; // good u is Foo
}
if (#staticField in u) {
u; // good u is typeof Foo
}
if (#staticMethod in u) {
u; // good u is typeof Foo
}
}
}
if (#field in fb) {
fb; // good fb is Foo
}
else {
fb; // good fb is Bar
}
if (#field in fs) {
fs; // good fs is FooSub
}
else {
fs; // good fs is never
}
if (#field in b) {
b; // good b is 'Bar & Foo'
}
else {
b; // good b is Bar
}
if (#field in fsb) {
fsb; // good fsb is FooSub
}
else {
fsb; // good fsb is Bar
}
if (#field in fsfb) {
fsfb; // good fsfb is 'Foo | FooSub'
}
else {
fsfb; // good fsfb is Bar
}
class Nested {
m(v) {
if (#field in v) {
v; // good v is Foo
}
}
}
}
}
class FooSub extends Foo {
subTypeOfFoo = true;
}
class Bar {
notFoo = true;
}
function badSyntax(v) {
return #field in v; // Bad - outside of class
}
|