Next.js next/image 'image' 隐式具有 'any' 类型 - 如何修复?

问题描述 投票:0回答:1

我是 Next.js 和 TypeScript 的新手,似乎找不到任何有关如何解决此问题的资源:

import Image from 'next/image';

export default function Item({ image }) { // <-- parameter image needs a 'type' definition?
  ...
}

但是上面给出了关于参数的通知/警告/错误

image

Binding element 'image' implicitly has an 'any' type.ts(7031). (parameter) image: any

我知道 TS 需要定义/声明变量类型,但我确实不知道 NextJS Image 应该是什么“类型”,而且我也知道给它

any
类型也不是可行的方法?我该如何解决这个问题?

我尝试了以下方法但不起作用:

export default function MealItem({ image: Image }) { // <-- tried the `image: Image` syntax

是否有人有任何资源、文档或首选位置来搜索参数/函数的正确类型以及与之相伴的正确语法来消除这些警告?

提前致谢。

javascript reactjs typescript next.js frontend
1个回答
0
投票

消息说你必须明确定义

{ image }

的类型

没有类型定义,默认使用的是

any
,因为 tsconfig 中默认为 true
https://www.typescriptlang.org/tsconfig/#noImplicitAny
您必须明确定义参数的类型,或者如果您确实想要任何指定它

noImplicitAny

© www.soinside.com 2019 - 2024. All rights reserved.