You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报错:number类型无法赋值给undefined类型问题求解

解决TypeScript错误:Type 'number' is not assignable to type 'undefined'

针对你在<Image>组件scale={[1.618, 1, 1]}属性最后一个数字处遇到的TS错误,提供以下几种解决方法:

方法1:使用THREE.Vector3实例替代数组

将数组形式的scale值替换为Vector3对象,贴合Three.js及@react-three/drei的类型定义:

<Image
  ref={ref}
  url={url}
  scale={new THREE.Vector3(1.618, 1, 1)}
  side={THREE.DoubleSide}
  alt=""
/>

方法2:类型断言强制指定数组类型

通过类型断言告诉TypeScript数组的第三个元素合法,适配组件的类型定义:

<Image
  ref={ref}
  url={url}
  scale={[1.618, 1, 1] as [number, number, number]}
  side={THREE.DoubleSide}
  alt=""
/>

方法3:升级@react-three/drei版本

该错误可能源于旧版本的类型定义缺陷,升级到最新版可修复类型不兼容问题:

npm install @react-three/drei@latest
# 或使用yarn
yarn add @react-three/drei@latest

另外,你的ref类型定义为Mesh是合理的,<Image>组件返回的正是Mesh实例,无需调整。

内容的提问来源于stack exchange,提问作者Taha Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:02:38