일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Spa
- refactoring
- CRUD w ReactQuery
- CS
- TypeScript
- 멀티스레드
- 회고
- wanted-preonboarding-course
- 짝선배 짝후배 매칭 웹 개발 회고
- teave
- 멀티프로세스
- error
Archives
- Today
- Total
깊고 넓은 삽질
[typescript / error] unknown 타입으로 전달되는 객체의 속성 사용하기 본문
🏁 목적 | jwt 토큰을 디코딩한 객체의 특정 value값을 storage에 넣기
◼ 토큰을 디코딩해서 디코딩된 토큰 객체의 user_id 값을 사용하고 싶었다.
❌ 문제 | 객체의 속성을 못 찾음
그럼 디코딩된 토큰이 내가 생각하는 객체인지 확인해주는 타입가드를 넣으면 어떨까?
✅ 해결 | 사용자 정의 타입가드 함수 사용
구글링 해보니 is라는 ts문법으로 사용자정의 타입가드를 할 수 있단다
https://bobbyhadz.com/blog/typescript-check-if-unknown-has-property
Check if Value with Unknown Type contains Property in TS | bobbyhadz
Use a user-defined type guard to check if a value with `unknown` type contains a property in TypeScript. The user-defined type guard consists of a function that checks if the specific property is contained in the object and returns a predicate.
bobbyhadz.com
Comments