我知道可选链应该足够了,但我在这里做得有点过头了,试图满足TypeScript:
const ref = useRef()
if (ref !== undefined) {
if(ref.hasOwnProperty('current')) {
if (ref.current !== undefined && ref.current !== null)
console.log(ref?.current?.getBoundingClientRect())
}
}
错误:
TS2339: Property 'getBoundingClientRect' does not exist on type 'never'.
我怀念我不打字的日子。除@ts-ignore
以外的任何解决方案
发布于 2021-02-16 09:27:49
您只需向useRef提供一个元素类型
const Test = () => {
const ref = useRef<HTMLInputElement>(null);
const rect = ref?.current?.getBoundingClientRect();
}
https://stackoverflow.com/questions/66216231
复制相似问题