Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18947,6 +18947,12 @@ namespace ts {
return false;
}

function containsTruthyCheck(source: Node, target: Node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here that this specifically deals the binary operator precedence. would also be useful. The implementation looks good here, but this function is named something much more general than it actually is and that's going to be confusing in the future.

return isMatchingReference(source, target) ||
(target.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>target).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken &&
(isMatchingReference(source, (<BinaryExpression>target).left) || isMatchingReference(source, (<BinaryExpression>target).right)));
}

function getAccessedPropertyName(access: AccessExpression): __String | undefined {
return access.kind === SyntaxKind.PropertyAccessExpression ? access.name.escapedText :
isStringOrNumericLiteralLike(access.argumentExpression) ? escapeLeadingUnderscores(access.argumentExpression.text) :
Expand Down Expand Up @@ -20343,15 +20349,22 @@ namespace ts {
if (type.flags & TypeFlags.Any && literal.text === "function") {
return type;
}
if (assumeTrue && type.flags & TypeFlags.Unknown && literal.text === "object") {
// The pattern x && typeof x === 'object', where x is of type unknown, narrows x to type object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to note here that you don't need to check both directions because the typeof x === "object" && x is narrowed as two separate steps. I don't think that would be obvious to a casual reader.

if (typeOfExpr.parent.parent.kind === SyntaxKind.BinaryExpression) {
const expr = <BinaryExpression>typeOfExpr.parent.parent;
if (expr.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken && expr.right === typeOfExpr.parent && containsTruthyCheck(reference, expr.left)) {
return nonPrimitiveType;
}
}
return getUnionType([nonPrimitiveType, nullType]);
}
const facts = assumeTrue ?
typeofEQFacts.get(literal.text) || TypeFacts.TypeofEQHostObject :
typeofNEFacts.get(literal.text) || TypeFacts.TypeofNEHostObject;
return getTypeWithFacts(assumeTrue ? mapType(type, narrowTypeForTypeof) : type, facts);

function narrowTypeForTypeof(type: Type) {
if (type.flags & TypeFlags.Unknown && literal.text === "object") {
return getUnionType([nonPrimitiveType, nullType]);
}
// We narrow a non-union type to an exact primitive type if the non-union type
// is a supertype of that primitive type. For example, type 'any' can be narrowed
// to one of the primitive types.
Expand Down
30 changes: 30 additions & 0 deletions tests/baselines/reference/narrowingTruthyObject.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
tests/cases/compiler/narrowingTruthyObject.ts(3,9): error TS2531: Object is possibly 'null'.


==== tests/cases/compiler/narrowingTruthyObject.ts (1 errors) ====
function foo(x: unknown, b: boolean) {
if (typeof x === 'object') {
x.toString();
~
!!! error TS2531: Object is possibly 'null'.
}
if (typeof x === 'object' && x) {
x.toString();
}
if (x && typeof x === 'object') {
x.toString();
}
if (b && x && typeof x === 'object') {
x.toString();
}
if (x && b && typeof x === 'object') {
x.toString();
}
}

// Repro from #36870

function f1(x: unknown): any {
return x && typeof x === 'object' && x.hasOwnProperty('x');
}

49 changes: 49 additions & 0 deletions tests/baselines/reference/narrowingTruthyObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [narrowingTruthyObject.ts]
function foo(x: unknown, b: boolean) {
if (typeof x === 'object') {
x.toString();
}
if (typeof x === 'object' && x) {
x.toString();
}
if (x && typeof x === 'object') {
x.toString();
}
if (b && x && typeof x === 'object') {
x.toString();
}
if (x && b && typeof x === 'object') {
x.toString();
}
}

// Repro from #36870

function f1(x: unknown): any {
return x && typeof x === 'object' && x.hasOwnProperty('x');
}


//// [narrowingTruthyObject.js]
"use strict";
function foo(x, b) {
if (typeof x === 'object') {
x.toString();
}
if (typeof x === 'object' && x) {
x.toString();
}
if (x && typeof x === 'object') {
x.toString();
}
if (b && x && typeof x === 'object') {
x.toString();
}
if (x && b && typeof x === 'object') {
x.toString();
}
}
// Repro from #36870
function f1(x) {
return x && typeof x === 'object' && x.hasOwnProperty('x');
}
68 changes: 68 additions & 0 deletions tests/baselines/reference/narrowingTruthyObject.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
=== tests/cases/compiler/narrowingTruthyObject.ts ===
function foo(x: unknown, b: boolean) {
>foo : Symbol(foo, Decl(narrowingTruthyObject.ts, 0, 0))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24))

if (typeof x === 'object') {
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))

x.toString();
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
}
if (typeof x === 'object' && x) {
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))

x.toString();
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
}
if (x && typeof x === 'object') {
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))

x.toString();
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
}
if (b && x && typeof x === 'object') {
>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))

x.toString();
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
}
if (x && b && typeof x === 'object') {
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>b : Symbol(b, Decl(narrowingTruthyObject.ts, 0, 24))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))

x.toString();
>x.toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 0, 13))
>toString : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))
}
}

// Repro from #36870

function f1(x: unknown): any {
>f1 : Symbol(f1, Decl(narrowingTruthyObject.ts, 16, 1))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 20, 12))

return x && typeof x === 'object' && x.hasOwnProperty('x');
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 20, 12))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 20, 12))
>x.hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --))
>x : Symbol(x, Decl(narrowingTruthyObject.ts, 20, 12))
>hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --))
}

101 changes: 101 additions & 0 deletions tests/baselines/reference/narrowingTruthyObject.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
=== tests/cases/compiler/narrowingTruthyObject.ts ===
function foo(x: unknown, b: boolean) {
>foo : (x: unknown, b: boolean) => void
>x : unknown
>b : boolean

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

x.toString();
>x.toString() : string
>x.toString : () => string
>x : object | null
>toString : () => string
}
if (typeof x === 'object' && x) {
>typeof x === 'object' && x : false | object | null
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
>x : object | null

x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (x && typeof x === 'object') {
>x && typeof x === 'object' : boolean
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"

x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (b && x && typeof x === 'object') {
>b && x && typeof x === 'object' : boolean
>b && x : unknown
>b : boolean
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"

x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (x && b && typeof x === 'object') {
>x && b && typeof x === 'object' : boolean
>x && b : boolean
>x : unknown
>b : boolean
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"

x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
}

// Repro from #36870

function f1(x: unknown): any {
>f1 : (x: unknown) => any
>x : unknown

return x && typeof x === 'object' && x.hasOwnProperty('x');
>x && typeof x === 'object' && x.hasOwnProperty('x') : boolean
>x && typeof x === 'object' : boolean
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
>x.hasOwnProperty('x') : boolean
>x.hasOwnProperty : (v: string | number | symbol) => boolean
>x : object
>hasOwnProperty : (v: string | number | symbol) => boolean
>'x' : "x"
}

25 changes: 25 additions & 0 deletions tests/cases/compiler/narrowingTruthyObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @strict: true

function foo(x: unknown, b: boolean) {
if (typeof x === 'object') {
x.toString();
}
if (typeof x === 'object' && x) {
x.toString();
}
if (x && typeof x === 'object') {
x.toString();
}
if (b && x && typeof x === 'object') {
x.toString();
}
if (x && b && typeof x === 'object') {
x.toString();
}
}

// Repro from #36870

function f1(x: unknown): any {
return x && typeof x === 'object' && x.hasOwnProperty('x');
}