Any difference between type assertions and the newer `as` operator in TypeScript?

后端 未结 2 552
忘了有多久
忘了有多久 2020-12-01 08:33

Is there any difference between what the TypeScript spec calls a type assertion:

var circle =  createShape(\"circle\");

And t

2条回答
  •  无人及你
    2020-12-01 09:19

    The difference is that as Circle works in TSX files, but conflicts with JSX syntax. as was introduced for this reason.

    For example, the following code in a .tsx file:

    var circle =  createShape("circle");
    

    Will result in the following error:

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

    However, as Circle will work just fine.

    Use as Circle from now on. It's the recommended syntax.

提交回复
热议问题