活字体の仕様は、型アサーションと呼んでいるものの間に違いはあります:タイプアサーションとTypeScriptの新しい `as`演算子との違いはありますか?
var circle = <Circle> createShape("circle");
そしてneweras
オペレータ:
var circle = createShape("circle") as Circle;
どちらも通常はコンパイル時にキャストするために使用されていますか?
活字体の仕様は、型アサーションと呼んでいるものの間に違いはあります:タイプアサーションとTypeScriptの新しい `as`演算子との違いはありますか?
var circle = <Circle> createShape("circle");
そしてneweras
オペレータ:
var circle = createShape("circle") as Circle;
どちらも通常はコンパイル時にキャストするために使用されていますか?
違いは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
を使用してください。これは推奨される構文です。 Wiki pageから
は: "何が[1.6]活字体の新機能":
New
.tsx
file extension andas
operatorTypeScript 1.6 introduces a new
.tsx
file extension. This extension does two things: it enables JSX inside of TypeScript files, and it makes the newas
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;