Fetch : Argument of type 'string | undefined' is not assignable to parameter of type 'RequestInfo'

Viewed 1843

Im receiving the following error in the "fetchUrl" argument in the fetch function

Argument of type 'string | undefined' is not assignable to parameter of type 'RequestInfo'.

In this function

let fetchUrl = getBaseUrlNative( process.env.APP_DATA, `/v2/marketing/app/file/${file}?cookies=${cookieSess}&expires=${Date.now()}`)

    await fetch(fetchUrl, { method: 'get' , cache:"no-store" , headers: {
        'Cache-Control': 'no-cache'
      }})
         .then(res => res.blob())
         .then(res => {  .......

getBaseUrlNative is

export const getBaseUrlNative = (eUrl : string | undefined, ext : string | undefined) => {
    try {
        if (!eUrl) throw new Error("Error .")
        return ext ? eUrl+ext : eUrl;
    } catch (err) {
        console.log(err)
    }
}
2 Answers

It's because your env variable comes as undefined before you use it.

Use type assertion with the keyword:

as

So something like:

getBaseUrlNative( process.env.APP_DATA as string, ...)

This should work just fine

Use the any data type in the fetch function to disallow type checking.

let fetchUrl = getBaseUrlNative( process.env.APP_DATA, `/v2/marketing/app/file/${file}?cookies=${cookieSess}&expires=${Date.now()}`)

    await fetch(<any>fetchUrl, { method: 'get' , cache:"no-store" , headers: {
        'Cache-Control': 'no-cache'
      }})
         .then(res => res.blob())
         .then(res => {  .......

Related