프로젝트/Toy Project

(0708) axios 모듈화 중 Typescript 에러 해결

수e 2022. 7. 8. 11:56
 
1
2
3
4
5
6
7
8
import axios, { AxiosInstance } from 'axios';
 
export const customAxios: AxiosInstance = axios.create({
  baseURL: 'http://localhost:8080'// 기본 서버 주소 입력
  headers: {
    Authorization: localStorage.getItem('jwtToken' || ""),
  },
});
cs

에러 메시지 : Type ' string | null' is not assignable to type string|number|boolean 

 

수정 ㄱ

1
2
3
4
5
6
7
8
9
import axios, { AxiosInstance } from 'axios';
const jwtToken = localStorage.getItem("jwtToken")
 
export const customAxios: AxiosInstance = axios.create({
  baseURL: 'http://localhost:8080'// 기본 서버 주소 입력
  headers: {
    Authorization: jwtToken!,
  },
});
cs

 

참고 ㄱ

 

https://blog.toycrane.xyz/typescript%EC%97%90%EC%84%9C-undefined-value-%EC%B2%98%EB%A6%AC%ED%95%98%EA%B8%B0-181035b5ee47

 

typescript에서 undefined value 처리하기

Typescript로 개발 하다보면, type이 맞지 않아 컴파일 에러가 자주 발생하곤 합니다. 그래서 여러 글들을 찾아보던 중 괜찮은 글이 있어서 번역을 해보았습니다. 번역이 매끄럽지 않더라도, 많은 이

blog.toycrane.xyz