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.

function foo(x: number | string) {<br />

if (typeof x === "string") {<br />

return x.length; // x has type string here<br />

}<br />

else {<br />

return x + 1; // x has type number here<br />

}<br />

}<br />

The type of a variable or parameter is narrowed in the following situations:<br />

<br />

<br />

<br />

<br />

<br />

<br />

In the true branch statement of an 'if' statement, the type of a variable or parameter is narrowed<br />

by a type guard in the 'if' condition when true, provided the true branch statement contains no<br />

assignments to the variable or parameter.<br />

In the false branch statement of an 'if' statement, the type of a variable or parameter is narrowed<br />

by a type guard in the 'if' condition when false, provided the false branch statement contains no<br />

assignments to the variable or parameter.<br />

In the true expression of a conditional expression, the type of a variable or parameter is narrowed<br />

by a type guard in the condition when true, provided the true expression contains no assignments<br />

to the variable or parameter.<br />

In the false expression of a conditional expression, the type of a variable or parameter is narrowed<br />

by a type guard in the condition when false, provided the false expression contains no<br />

assignments to the variable or parameter.<br />

In the right operand of a && operation, the type of a variable or parameter is narrowed by a type<br />

guard in the left operand when true, provided the right operand contains no assignments to the<br />

variable or parameter.<br />

In the right operand of a || operation, the type of a variable or parameter is narrowed by a type<br />

guard in the left operand when false, provided the right operand contains no assignments to the<br />

variable or parameter.<br />

A type guard is simply an expression that follows a particular pattern. The process of narrowing the type<br />

of a variable x by a type guard when true or when false depends on the type guard as follows:<br />

<br />

<br />

A type guard of the form x instanceof C, where x is not of type Any, C is of a subtype of the<br />

global type 'Function', and C has a property named 'prototype'<br />

o when true, narrows the type of x to the type of the 'prototype' property in C provided it is<br />

a subtype of the type of x, or, if the type of x is a union type, removes from the type of x<br />

all constituent types that aren't subtypes of the type of the 'prototype' property in C, or<br />

o when false, has no effect on the type of x.<br />

A type guard of the form typeof x === s, where s is a string literal with the value 'string',<br />

'number', or 'boolean',<br />

o when true, narrows the type of x to the given primitive type provided it is a subtype of the<br />

type of x, or, if the type of x is a union type, removes from the type of x all constituent<br />

types that aren't subtypes of the given primitive type, or<br />

82

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

Saved successfully!

Ooh no, something went wrong!