首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型"number“不能赋值给类型void | Destructor

类型"number“不能赋值给类型void | Destructor
EN

Stack Overflow用户
提问于 2021-11-09 21:17:55
回答 2查看 78关注 0票数 0

我有以下两个组件:

DraggableGameStart

代码语言:javascript
复制
import DraggableGameAnswers from './DraggableGameAnswers';
import { useRef } from 'react';

function DraggableGameStart() {
    const draggableGame = useRef<HTMLDivElement>(null);

    return (
        <div className="draggable-game-content">
            <div className="draggable-game" ref={draggableGame}>
                    <DraggableGameAnswers elementBounds={draggableGame}/>
                </div>
            </div>
        </div>
    );
}

export default DraggableGameStart;

和DraggableGameAnswers

代码语言:javascript
复制
import { RefObject } from 'react';
import { useEffect } from 'react';

type DraggableGameAnswersProps = {
    elementBounds: RefObject<HTMLDivElement>
}

function DraggableGameAnswers(props: DraggableGameAnswersProps) {
    let boundsArray: Array<number>[];
    useEffect(
        () => 
        boundsArray.push(props.elementBounds.current?.getBoundingClientRect().top)
    )  
    
    return (
        <div className="draggable-game-answers">
            test
        </div>
    );
}

export default DraggableGameAnswers;

但是我得到了一些错误:

代码语言:javascript
复制
Type number is not assignable to type 'void | Destructor' ;
Argument of type 'number | undefined' is not assignable to parameter of type 'number[]'.
  Type 'undefined' is not assignable to type 'number[]'.

我真的不明白为什么。

为什么会抛出这些错误,我如何修复它们?

编辑:

我根据一条评论改成了这行

代码语言:javascript
复制
let boundsArray: Array<number>;

什么都没有改变,只是第二个错误变成了这样:

代码语言:javascript
复制
Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.
EN

回答 2

Stack Overflow用户

发布于 2021-11-09 22:40:33

此错误对于您阅读和理解此错误非常重要。

代码语言:javascript
复制
Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.

你会注意到这个值:

代码语言:javascript
复制
props.elementBounds.current?.getBoundingClientRect().top

并不总是一个数字。如果current属性为undefined,那么您将获得undefined。这就是为什么这个值的类型是number | undefined

你把它推送到一个不接受undefinednumber数组中,所以你会得到这个错误。

要修复它,您只需在将其推送到数组之前检查该值是否不是nullundefined

代码语言:javascript
复制
    let boundsArray: Array<number>;
    useEffect(() => {
      const value = props.elementBounds.current?.getBoundingClientRect().top
      if (value != null) {
        boundsArray.push(value) // works
      }
    })  

Working example

票数 0
EN

Stack Overflow用户

发布于 2021-11-09 21:57:04

这个错误通常意味着你正在传递一个不适合你为组件定义的typescript接口的属性。我认为在这种情况下你要找的是转发的ref,而不是传递一个普通的道具。它可能看起来像这样:

代码语言:javascript
复制
import { React, useEffect } from 'react';

const DraggableGameAnswers = React.forwardRef((props, ref) {
    let boundsArray: Array<number>[];
    useEffect(
        () => 
        boundsArray.push(ref.current?.getBoundingClientRect().top)
    )  
    
    return (
        <div className="draggable-game-answers">
            test
        </div>
    );
})

export default DraggableGameAnswers;

然后,如果它不能解决问题,那么像<DraggableGameAnswers ref={draggableGame}/>这样调用它至少可以为您指明一个不同的方向。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69905223

复制
相关文章

相似问题

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