From 9190a2c4e8a9c5b87a3843d5268a3142d3406adb Mon Sep 17 00:00:00 2001 From: kingwl Date: Wed, 1 Apr 2020 16:28:48 +0800 Subject: [PATCH] Add contextual types infer --- src/compiler/checker.ts | 2 ++ .../esnext/logicalAssignment/logicalAssignment5.ts | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 tests/cases/conformance/esnext/logicalAssignment/logicalAssignment5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4b52f2faab0..89ee4693ffe 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22365,6 +22365,8 @@ namespace ts { return contextSensitive === true ? getTypeOfExpression(left) : contextSensitive; case SyntaxKind.BarBarToken: case SyntaxKind.QuestionQuestionToken: + case SyntaxKind.BarBarEqualsToken: + case SyntaxKind.QuestionQuestionEqualsToken: // When an || expression has a contextual type, the operands are contextually typed by that type, except // when that type originates in a binding pattern, the right operand is contextually typed by the type of // the left operand. When an || expression has no contextual type, the right operand is contextually typed diff --git a/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment5.ts b/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment5.ts new file mode 100644 index 00000000000..08176cce75b --- /dev/null +++ b/tests/cases/conformance/esnext/logicalAssignment/logicalAssignment5.ts @@ -0,0 +1,7 @@ +// @strict: true +// @target: esnext, es2020, es2015 + +function foo (f: (a: number) => void | undefined) { + f ??= (a => a++) + f ||= (a => a++) +}