2015-11-03 12 views

risposta

48

La differenza è che as Circle funziona in file TSX, ma <Circle> è in conflitto con la sintassi JSX. as è stato introdotto per questo motivo.

Ad esempio, il seguente codice in un file .tsx:

var circle = <Circle> createShape("circle"); 

Sarà causare il seguente errore:

error TS17002: Expected corresponding JSX closing tag for 'Circle'.

Tuttavia, as Circle funzionano bene.

Utilizzare as Circle da ora in poi. È la sintassi consigliata.

13

Da Wiki page: "Cosa c'è di nuovo a macchina [1.6]":

New .tsx file extension and as operator

TypeScript 1.6 introduces a new .tsx file extension. This extension does two things: it enables JSX inside of TypeScript files, and it makes the new as operator the default way to cast (removing any ambiguity between JSX expressions and the TypeScript prefix cast operator). For example:

var x = <any> foo; 
// is equivalent to: 
var x = foo as any;