jsx类型为函数时,如何禁用道具检查?

时间:2019-06-13 03:39:47

标签: reactjs typescript

在tsconfig.json中将jsx设置为react并将jsxFactory设置为fn时, 还有一些错误:

a.tsx

import fn from "./fn";
import delegate from "./delegate";

class A {
    constructor(point: { x: number, y: number }){
        console.log(x, y);

        // ...
    }
}

delegate(
    <A target={document.getElementById("myId")} />
);

delegate.ts

export default function delegate({ type, props: { target } }){
    target.onClick = (e) => {
        new type(e.clickX, e.clickY);
    };
};

错误:Type '{ target: HTMLElement; }' is not assignable to type '{ x: number;, y: number; }'Property 'target' does not exist on type '{ x: number; y: number; }'

我如何禁用道具检查? 不必关心代码的逻辑,因为,这只是一个例子。

2 个答案:

答案 0 :(得分:1)

您可以将道具声明为any,然后传播(...)道具:

delegate(
    <A {...({ target: document.getElementById("myId") } as any)} />
);

答案 1 :(得分:0)

代码如下:

jsx-namespace.d.ts

declare namespace JSX {
     // ...
    interface IntrinsicAttributes {
        [attributeName: string]: any
    }
    // ...
}

typescript 3.4.5