2

In this fragment I Have the next error :

Type 'string | null' is not assignable to type 'string'.
  Type 'null' is not assignable to type 'string'.  TS2322
export async function GetCertainCoinByType(coinId: string) {
  const response = await axios.get(URLofCertainCoins + `certain/${coinId}`, {
      headers : {
          token : localStorage.getItem('token'),
      }
  });
  return response;
}
1
  • 4
    Localestorage getItem can potentially return null so you need to check for this.. I personally would throw an exception, as calling this fn without a token would seem invalid. Commented Dec 13, 2021 at 15:57

2 Answers 2

2

That's because the return type of Local storage is string | null, and not just string. To avoid this exception, you can first get the item from local storage and then use it like:

export async function GetCertainCoinByType(coinId: string) {
  let tokenFromStorage = localStorage.getItem('token')
  if (!tokenFromStorage ) { 
    throw new Error("no token supplied"); 
  }
  const response = await axios.get(URLofCertainCoins + `certain/${coinId}`, 
  {
      headers : {
          token : tokenFromStorage 
      }
  });
  return response;
}
Sign up to request clarification or add additional context in comments.

4 Comments

It's nearly there, you forgot to put the bounds check in. eg. on the next line after the let, just put something like -> if (tokenFromStorage === null) { throw new Error("no token supplied"); } or something like that. This will then make TS happy.
I tried to point out the use of avoiding the error, as there will be token present in OP's perspective. But that's not true, agreed. Updated the answer. Thanks for pointing out @CollinD and Keith
Unfortunately your update of calling getItem will cause the error again.. :( Using your original let, and doing the check after the let will fix that. That because getItem will always return string | null, but after doing a bounds check on the let it will become string..
Not sure why I am in a hurry, just did that
1

First, check whether token is null, and if it is, exit the function.

Now response is a Promise anyway, since it's returned by an async function. You don't need to await Axios, then re-convert it to a Promise afterwards. You can directly return axios.get(...) and since this removes the only await, it turns out you don't even need the async/await syntax at all here.

export function GetCertainCoinByType(coinId: string): Promise<any> {

  const token:string|null = localStorage.getItem('token');

  if(!token){
    console.log("No token!");
    return ;
  }

  return axios.get(URLofCertainCoins + `certain/${coinId}`, {
      headers : { token }
  });
}

3 Comments

There really is no reason to put token:string|null, implicit type is fine here. It's the bounds check that's important..
My goal is to be explicit for the sake of demonstration and for the answer to be clear :)
Ok, that makes sense.. Just wanted to make sure other's reading this don't assume been explicit here is what's fixing the issue, rather than the bounds check..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.