“类型“never”上不存在属性“childfocusfn”。ts(2339)”错误的解决方法
在 react 中使用 forwardref 和 useimperativehandle 时,如果不正确地指定它们的类型,可能会遇到错误:“类型“never”上不存在属性“childfocusfn”。ts(2339)”。
要避免此错误,需要在使用 useref 创建 ref 时指定正确的类型。在给定示例中,应将 ref 指定为具有 childfocusfn 属性的类型,如下所示:
const ref = useRef<{ childFocusFn: () => void }>(null);
这将指示 typescript 将 ref.current 解释为具有 childfocusfn 属性的对象,从而避免前面提到的错误。