当在.tsx
文件中强制转换时,编译器假定它是JSX,例如:
(<HtmlInputElement> event.target).value
给出了一个错误
JSX元素类型"HtmlInputElement"不是JSX元素的构造函数
如何在.tsx
文件中转换TypeScript?
当在.tsx
文件中强制转换时,编译器假定它是JSX,例如:
(<HtmlInputElement> event.target).value
给出了一个错误
JSX元素类型"HtmlInputElement"不是JSX元素的构造函数
如何在.tsx
文件中转换TypeScript?
将as
运算符引入TypeScript 1.6以替换.tsx
个文件中的强制转换,例如:
(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)