|
| 1 | +=== tests/cases/compiler/narrowingTruthyObject.ts === |
| 2 | +function foo(x: unknown, b: boolean) { |
| 3 | +>foo : Symbol(foo, Decl(narrowingTruthyObject.ts, 0, 0)) |
| 4 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 5 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 6 | + |
| 7 | + if (typeof x === 'object') { |
| 8 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 9 | + |
| 10 | + x.toString(); |
| 11 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 12 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 13 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 14 | + } |
| 15 | + if (typeof x === 'object' && x) { |
| 16 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 17 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 18 | + |
| 19 | + x.toString(); |
| 20 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 21 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 22 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 23 | + } |
| 24 | + if (x && typeof x === 'object') { |
| 25 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 26 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 27 | + |
| 28 | + x.toString(); |
| 29 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 30 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 31 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 32 | + } |
| 33 | + if (b && x && typeof x === 'object') { |
| 34 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 35 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 36 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 37 | + |
| 38 | + x.toString(); |
| 39 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 40 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 41 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 42 | + } |
| 43 | + if (x && b && typeof x === 'object') { |
| 44 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 45 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 46 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 47 | + |
| 48 | + x.toString(); |
| 49 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 50 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 51 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 52 | + } |
| 53 | + if (x && b && b && typeof x === 'object') { |
| 54 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 55 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 56 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 57 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 58 | + |
| 59 | + x.toString(); |
| 60 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 61 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 62 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 63 | + } |
| 64 | + if (b && b && x && b && b && typeof x === 'object') { |
| 65 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 66 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 67 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 68 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 69 | +>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24)) |
| 70 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 71 | + |
| 72 | + x.toString(); |
| 73 | +>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 74 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13)) |
| 75 | +>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Repro from #36870 |
| 80 | + |
| 81 | +function f1(x: unknown): any { |
| 82 | +>f1 : Symbol(f1, Decl(narrowingTruthyObject.ts, 22, 1)) |
| 83 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 26, 12)) |
| 84 | + |
| 85 | + return x && typeof x === 'object' && x.hasOwnProperty('x'); |
| 86 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 26, 12)) |
| 87 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 26, 12)) |
| 88 | +>x.hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --)) |
| 89 | +>x : Symbol(x, Decl(narrowingTruthyObject.ts, 26, 12)) |
| 90 | +>hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --)) |
| 91 | +} |
| 92 | + |
0 commit comments