typescriptaxiosreact-typescript

Axios headers get, Cannot invoke an object which is possibly 'null' or 'undefined'.ts(2723)


I get the file name from the content-disposition object in the response header, but typescript gives an error. Cannot invoke an object which is possibly 'null' or 'undefined'.ts(2723)

     const filename = response.headers.get('content-disposition')?.split('"')[1] || 'file';

Also this didnt work

     const filename = response?.headers?.get('content-disposition')?.split('"')[1] || 'file';

Thanks for helping.


Solution

  • In axios, you just do this: response?.headers['content-disposition']