将 onTouchTap 添加到 div 时 Typescript 出现语法错误

syntax error in Typescript when adding onTouchTap to div

之后,在 .tsx 文件中使用 Typescript
import injectTapEventPlugin = require('react-tap-event-plugin')
injectTapEventPlugin()

使用这个 JSX:

<div onTouchTap={ this.showFront }>

我收到语法错误:

error TS2339: Property 'onTouchTap' does not exist on type 'HTMLProps<HTMLDivElement>'.

尽管有

interface HTMLDivElement {

    onTouchTap: Function,

}

在同一个代码文件中,应该将onTouchTap合并到div在lib.d.ts

中使用的HTMLDivElement接口中

智能感知看到 onTouchStart 等,但没有看到 onTouchTap

是哪里不对,还是我做错了什么?

谢谢,

in the same code file, which should merge onTouchTap into the HTMLDivElement interface used by div in lib.d.ts

没有。因为您的文件是一个模块,因为您具有根级别 import (import injectTapEventPlugin)。这意味着它与全局命名空间断开连接

此外,您要扩展的界面不是 HTMLDivElement,而是 react.d.ts 中定义的 DOM 属性。

修复

您需要创建一个 vendor.d.ts 并将其放入:

declare module __React{
    interface DOMAttributes {
        onTouchTap?: Function,
    }
}

更多:https://basarat.gitbooks.io/typescript/content/docs/project/modules.html