gpt4 book ai didi

reactjs - TS2339 : Property 'getBoundingClientRect' does not exist on type 'never'

转载 作者:行者123 更新时间:2023-12-04 11:58:43 29 4
gpt4 key购买 nike

我知道可选链接应该足够了,但我在这里尝试满足 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 之外的任何解决方案

最佳答案

你只需要提供一个元素类型来 useRef

const Test = () => {
const ref = useRef<HTMLInputElement>(null);
const rect = ref?.current?.getBoundingClientRect();
}

关于reactjs - TS2339 : Property 'getBoundingClientRect' does not exist on type 'never' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66216231/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com