[Nextjs + Typescript] velog 클론코딩/[ Nextjs + Typescript ] 나를 위한 기록
12. [Ts] unknown, void, never
tnqlscho 95
2023. 2. 23. 23:58
unknown
공공 api를 사용하거나 프로젝트때 api에서 던져주는 값의 type을 미리 알지못할때 쓴다.
let a = unknown;
if(typeof a === ‘number’){
let b = a + 1
}
if(typeof a === ‘string’){
let b = a.toUpperCase();
}
그래서 if(typeof a === ‘string’)하면 typescript가 타입 확인작업을 강제로 한다고한다.
void
Void : 비어있는, 없는
Function hello(){
console.log(‘x’)
}
Const a = hello();
a. toUpperCase()
함수 hello의 리턴값을 담은 a는 비어있기때문에 viod로 인식해서
리턴값이 없는 a에 무언가를 하려고하면 void라고 에러가 난다.
never
함수가 절대 return 하지 않을 때 발생한다.
*출처 [노마드코더 ts 강의]
https://nomadcoders.co/typescript-for-beginners/lectures/3672