How to use a TypeScript cast with JSX/TSX

和自甴很熟 提交于 2020-07-02 11:25:56

问题


When casting in a .tsx file, the compiler assumes it to be JSX, e.g.:

(<HtmlInputElement> event.target).value

gives an error

JSX element type 'HtmlInputElement' is not a constructor function for JSX elements

How do you cast TypeScript in a .tsx file?


回答1:


The as operator was introduced to TypeScript 1.6 to replace casts in .tsx files, e.g.:

(event.target as HTMLInputElement).value

The TypeScript wiki explains the 1.6 changes: it makes the new as operator the default way to cast (removing any ambiguity between JSX expressions and the TypeScript prefix cast operator)



来源:https://stackoverflow.com/questions/37613981/how-to-use-a-typescript-cast-with-jsx-tsx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!