简体   繁体   English

“文件”类型的参数不能分配给“从不”类型的参数。 与 TypeScript 反应

[英]Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript

How can I set the correct type in useState so I can push image as File in array?如何在 useState 中设置正确的类型,以便可以将图像作为文件推送到数组中?

Here is my useState:这是我的使用状态:

const [colorsAndImages, setColorsAndImages] = useState([{ images: [], colors: '' }])

Here is where I'm trying to push the image but I'm getting an error:这是我试图推送图像但出现错误的地方:

const handleSetImage = (event: React.ChangeEvent<HTMLInputElement>, index: number) => {
        const file = event.target.files;
        const list = [...colorsAndImages]
        list[index].images.push(file?.item(0)!)
        setColorsAndImages(list)
    };

The error is on...(file?.item(0)!)错误是...(文件?。项目(0)!)

Argument of type 'File' is not assignable to parameter of type 'never'. “文件”类型的参数不能分配给“从不”类型的参数。

When using the useState hook without an explicit generic, TypeScript will try to infer the type from the initial passed value.当使用没有显式泛型的useState钩子时,TypeScript 将尝试从初始传递的值推断类型。

Since your initial value includes an empty array (for images ), TypeScript is unable to determine the type of the values that will be in that array and so assigns it the type never .由于您的初始值包含一个空数组(对于images ),因此 TypeScript 无法确定该数组中的值的类型,因此将其分配为never类型。

To fix this, explicitly specify the type of your state when you initialize it:要解决此问题,请在初始化时明确指定 state 的类型:

const [colorsAndImages, setColorsAndImages] = useState<{images: File[], colors: string}[]>([{ images: [], colors: '' }])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() =&gt; 状态属性)' - React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)' React Typescript - 类型参数不可分配给类型参数 - React Typescript - Argument of type is not assignable to parameter of type 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript Typescript,useContext(),错误:“never[]”类型的参数不可分配给类型参数 - Typescript, useContext(), error: Argument of type 'never[]' is not assignable to parameter of type 类型React:类型&#39;profile&#39;的参数不能分配给&#39;ComponentType类型的参数 <never> &#39; - Type React: Argument of type 'profile' is not assignable to parameter of type 'ComponentType<never>' Typescript/React-Native:'string | 类型的参数 null' 不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>' React Typescript - 类型参数不可分配给“SetStateAction”类型的参数<user | record<string, never> >'</user> - React Typescript - Argument of type is not assignable to parameter of type 'SetStateAction<User | Record<string, never>>' 类型参数不可分配给“SetStateAction”类型的参数<never[]> &#39;。 在反应中 - Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React Typescript 抱怨 React useState setter `Argument of type 'Dispatch <setstateaction<never[]> &gt;' 不可分配给参数</setstateaction<never[]> - Typescript complaining about React useState setter `Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM