Skip to content

Add the comparability relationship to the spec (attempt two) #20686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
44 changes: 35 additions & 9 deletions doc/spec/Expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ TypeScript extends the JavaScript expression grammar with the ability to assert

A type assertion expression consists of a type enclosed in `<` and `>` followed by a unary expression. Type assertion expressions are purely a compile-time construct. Type assertions are *not* checked at run-time and have no impact on the emitted JavaScript (and therefore no run-time cost). The type and the enclosing `<` and `>` are simply removed from the generated code.

In a type assertion expression of the form &lt; *T* > *e*, *e* is contextually typed (section [#contextually-typed-expressions]<!--4.23-->) by *T* and the resulting type of* e* is required to be assignable to *T*, or *T* is required to be assignable to the widened form of the resulting type of *e*, or otherwise a compile-time error occurs. The type of the result is *T*.
In a type assertion expression of the form &lt; *T* > *e*, *e* is contextually typed (section [#contextually-typed-expressions]<!--4.23-->) by *T* and the resulting type of* e* is required to be comparable to *T*, or *T* is required to be comparable to the widened form of the resulting type of *e*, or otherwise a compile-time error occurs. The type of the result is *T*.

Type assertions check for assignment compatibility in both directions. Thus, type assertions allow type conversions that *might* be correct, but aren't *known* to be correct. In the example

Expand Down Expand Up @@ -714,15 +714,41 @@ The example above converts the result of 'getValue()' to a string if it isn't a

### The &lt;, >, &lt;=, >=, ==, !=, ===, and !== operators { #the-comparison-operators }

These operators require one or both of the operand types to be assignable to the other. The result is always of the Boolean primitive type.
These operators require one or both of the operand types to be comparable to the other.
The result is always of the Boolean primitive type.

||Any|Boolean|Number|String|Other|
|:---:|:---:|:---:|:---:|:---:|:---:|
|Any|Boolean|Boolean|Boolean|Boolean|Boolean|
|Boolean|Boolean|Boolean||||
|Number|Boolean||Boolean|||
|String|Boolean|||Boolean||
|Other|Boolean||||Boolean|
| |Any |Boolean|Number |String |Other |
|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
|Any |Boolean|Boolean|Boolean|Boolean|Boolean|
|Boolean|Boolean|Boolean| | | |
|Number |Boolean| |Boolean| | |
|String |Boolean| | |Boolean| |
|Other |Boolean| | | |Boolean|

### ==, !=, ===, and !== operators { #the-equality-operators }

These operators require one or both of the operand types to be comparable to the other, or for at least operand to be the Null or Undefined type.
The result is always of the Boolean primitive type.

| |Any |Boolean|Number |String |Other |
|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
|Any |Boolean|Boolean|Boolean|Boolean|Boolean|
|Boolean|Boolean|Boolean| | | |
|Number |Boolean| |Boolean| | |
|String |Boolean| | |Boolean| |
|Other |Boolean| | | |Boolean|

Permitting operands of any type to be compared directly to the Null and Undefined type is the principal difference from [the comparison operators](#the-comparison-operators).
This can be useful for performing runtime checks on arguments passed in from unchecked JavaScript code.

```TypeScript
function greetPerson(person: Person) {
if (person == null) {
throw new Error("Expected 'person' to be defined.");
}
// ...
}
```

### The instanceof operator { #the-instanceof-operator }

Expand Down
2 changes: 1 addition & 1 deletion doc/spec/Statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Use of the 'with' statement in TypeScript is an error, as is the case in ECMAScr

## Switch Statements { #switch-statements }

In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from (section [#assignment-compatibility]<!--3.11.4-->) the type of the 'switch' expression.
In a 'switch' statement, each 'case' expression must be the Null type, the Undefined type, or a type that is comparable to or from (section [#comarability]) the type of the 'switch' expression.

## Throw Statements { #throw-statements }

Expand Down
Loading