diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9e56c58411930..d91e4a94bb766 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25289,6 +25289,7 @@ namespace ts { } function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { + debugger; if (hasMatchingArgument(callExpression, reference)) { const signature = assumeTrue || !isCallChain(callExpression) ? getEffectsSignature(callExpression) : undefined; const predicate = signature && getTypePredicateOfSignature(signature); @@ -25306,6 +25307,13 @@ namespace ts { } } } + + if (isPropertyAccessExpression(callExpression.expression)) { + const callAccess = callExpression.expression; + if (isIdentifier(callAccess.name) && callAccess.name.escapedText === "hasOwn" && callExpression.arguments.length === 2 && isStringLiteralLike(callExpression.arguments[1])) { + return narrowByInKeyword(type, callExpression.arguments[1].text as __String, assumeTrue); + } + } return type; } diff --git a/tests/cases/compiler/objectHasOwnNarrowingSimpleUnion.ts b/tests/cases/compiler/objectHasOwnNarrowingSimpleUnion.ts new file mode 100644 index 0000000000000..6a5a4acecc1ff --- /dev/null +++ b/tests/cases/compiler/objectHasOwnNarrowingSimpleUnion.ts @@ -0,0 +1,6 @@ +// @target: es2022 + +declare const abcd: { a: string, b: number } | { c: number, d: string }; +if (Object.hasOwn(abcd, "a")) { + abcd.b; +}