How to use a TypeScript cast with JSX/TSX

后端 未结 1 1302
萌比男神i
萌比男神i 2021-01-01 08:57

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

( event.target).value

gives an e

相关标签:
1条回答
  • 2021-01-01 09:18

    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)

    0 讨论(0)
提交回复
热议问题