Skip to content
Advertisement

Defining type for input file state: React+Typescript

I am getting the following error while I define the type of file object inside state, Argument of type 'null' is not assignable to parameter of type 'File | (() => File)'.ts. I am having a upload component, that basically takes care of selecting the file and sending its value to the parent component. I am trying to this in a typescript way and I am unable to get rid of this.

Link to the sandbox: https://codesandbox.io/s/react-hook-upload-oxqdp2?file=/src/Upload.tsx

Upload

JavaScript

Can someone help?

Advertisement

Answer

You should probably stick to undefined:

JavaScript

This is because when you set it, the value has the type File | undefined.

This will also force you to check the value before using it, since it could be undefined.

If you know that there will be a file, you can use an assertion with !:

JavaScript

and maybe keep the type as File | null, but even then I’d still use File | undefined because null is a bit quirky in JavaScript (typeof null === "object').

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement