首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TS2339: never类型上不存在属性“getBoundingClientRect”

TS2339: never类型上不存在属性“getBoundingClientRect”
EN

Stack Overflow用户
提问于 2021-02-16 06:31:35
回答 1查看 1.6K关注 0票数 6

我知道可选链应该足够了,但我在这里做得有点过头了,试图满足TypeScript:

代码语言:javascript
运行
复制
const ref = useRef()

    if (ref !== undefined) {
        if(ref.hasOwnProperty('current')) {
            if (ref.current !== undefined &&  ref.current !== null)
             console.log(ref?.current?.getBoundingClientRect())
        }
    }

错误:

代码语言:javascript
运行
复制
TS2339: Property 'getBoundingClientRect' does not exist on type 'never'.

我怀念我不打字的日子。除@ts-ignore以外的任何解决方案

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-16 09:27:49

您只需向useRef提供一个元素类型

代码语言:javascript
运行
复制
const Test = () => {
    const ref = useRef<HTMLInputElement>(null);
    const rect = ref?.current?.getBoundingClientRect();
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66216231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档