2015-11-03 10 views

回答

48

區別在於as Circle適用於TSX文件,但<Circle>與JSX語法衝突。爲此推出了as

例如,在一個.tsx文件下面的代碼:

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

將導致以下錯誤:

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

然而,as Circle會工作得很好。

從現在開始使用as Circle。這是推薦的語法。

13

Wiki page: 「什麼打字稿[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; 
相關問題