【发布时间】:2021-12-17 19:13:34
【问题描述】:
另一个 React + TypeScript 事件处理程序问题。
如何修复这种类型的错误?
const onResize = (
event: React.UIEvent<Window, "resize">,
) => {
console.log(event.target);
};
window.addEventListener("resize", onResize);
这会抛出
No overload matches this call.
Overload 1 of 2, '(type: "resize", listener: (this: Window, ev: UIEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(event: React.UIEvent<Window, "resize">) => void' is not assignable to parameter of type '(this: Window, ev: UIEvent) => any'.
Types of parameters 'event' and 'ev' are incompatible.
Type 'UIEvent' is missing the following properties from type 'UIEvent<Window, "resize">': nativeEvent, isDefaultPrevented, isPropagationStopped, persist
Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(event: React.UIEvent<Window, "resize">) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
Type '(event: React.UIEvent<Window, "resize">) => void' is not assignable to type 'EventListener'.
Types of parameters 'event' and 'evt' are incompatible.
Type 'Event' is missing the following properties from type 'UIEvent<Window, "resize">': detail, view, nativeEvent, isDefaultPrevented, and 2 more. TS2769
【问题讨论】:
标签: reactjs typescript dom-events