23.06.2015 Views

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

var shape = createShape(shapeKind);<br />

if (shape instanceof Circle) {<br />

var circle = shape;<br />

...<br />

}<br />

4.14 Unary Operators<br />

The subsections that follow specify the compile-time processing rules of the unary operators. In general, if<br />

the operand of a unary operator does not meet the stated requirements, a compile-time error occurs and<br />

the result of the operation defaults to type Any in further processing.<br />

4.14.1 The ++ and -- operators<br />

These operators, in prefix or postfix form, require their operand to be of type Any, the Number primitive<br />

type, or an enum type, and classified as a reference (section 4.1). They produce a result of the Number<br />

primitive type.<br />

4.14.2 The +, –, and ~ operators<br />

These operators permit their operand to be of any type and produce a result of the Number primitive<br />

type.<br />

The unary + operator can conveniently be used to convert a value of any type to the Number primitive<br />

type:<br />

function getValue() { ... }<br />

var n = +getValue();<br />

The example above converts the result of 'getValue()' to a number if it isn't a number already. The type<br />

inferred for 'n' is the Number primitive type regardless of the return type of 'getValue'.<br />

4.14.3 The ! operator<br />

The ! operator permits its operand to be of any type and produces a result of the Boolean primitive type.<br />

Two unary ! operators in sequence can conveniently be used to convert a value of any type to the Boolean<br />

primitive type:<br />

function getValue() { ... }<br />

var b = !!getValue();<br />

The example above converts the result of 'getValue()' to a Boolean if it isn't a Boolean already. The type<br />

inferred for 'b' is the Boolean primitive type regardless of the return type of 'getValue'.<br />

73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!