This project is read-only.

Contextually Typed Expressions

Topics: Language Specification
Feb 21, 2014 at 3:37 PM
Just wondering, why no contextual typing occurs (according to spec 4.19) in the following situations:
  1. In conditional operator (?:) third operand could be contextually typed by second operand.
  2. In arrow function expressions in form
    ArrowFormalParameters => AssignmentExpression
    AssignmentExpression could be contextually typed in the same way as return expressions.
I'm sure there is some logical explanation, but it evades me :)
Feb 21, 2014 at 7:33 PM
Edited Feb 21, 2014 at 7:34 PM
Regarding 1: It's not really clear that either of the second or third operand provide context for the other. For example, you should be able to reverse the condition and the two operands and get exactly the same behavior.

Regarding 2: The AssignmentExpression is contextually typed the same way as a return expression. The spec states (in 4.9.2) that an arrow function expression with an expression body is exactly equivalent to an arrow function expression with a single return statement, and this extends to contextual typing as well.
Feb 21, 2014 at 7:37 PM