๐ Narrowing
Narrowing์ ๊ฐ์ ํ์ ์ด ๋ ๊ตฌ์ฒด์ ์ผ๋ก ํ๋จ ๋ ์ ์๋๊ฒฝ์ฐ
ํ์ ์ ๋ ์ขํ์ ๊ตฌ์ฒด์ ์ผ๋ก ์ถ๋ก ํ ์ ์๋ ๊ธฐ์ ์ ๋๋ค.
์๋ฅผ ๋ค์ด ์ด๋ค ํจ์์ ๋งค๊ฐ๋ณ์ a๊ฐ string | number ๊ฐ์ ์ ๋์จ ํ์
์ผ ๋
ํ์ ์คํฌ๋ฆฝํธ์์ ํจ์๋ a.toLowerCase()๊ฐ์๊ฑด ์คํํด ์ฃผ์ง ์์ต๋๋ค.
๊ทธ ์ด์ ๋, a์ ์ด๋ค ํ์ ์ด ์ฌ์ง ์์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ ๋๋ค
์ด๋ฐ๊ฒฝ์ฐ Narrowing์ ํตํด์ ๊ตฌ์ฒด์ ์ผ๋ก ํ์ ์คํฌ๋ฆฝํธ์๊ฒ ์๋ ค์ค ์ ์๊ฒ ์ต๋๋ค
๐ก๏ธ Type Guards
๊ทธ๋ผ ์ด์ ํจ์๋ฅผ ๋ง๋ค๋ฉด์ a์ ์ด๋ป๊ฒ Narrowing์ ํ ์ง ์์๋ด ์๋ค.
function fn(a : string | number):string{
if(typeof a === 'string'){
return a.toUpperCase()
}
return a.toLocaleString()
}
fn('gg')
์ฌ์ค ํ์ ๊ฐ๋๋ผ๋๊ฒ ๋ญ๊ฐ ํน๋ณํ๊ฒ ์๋ค๊ธฐ๋ณด๋ค๋ typeof ์ฐ์ฐ์์ ์กฐ๊ฑด๋ฌธ์ ํตํ์ฌ ์์ธ์ฒ๋ฆฌํ๋ ๊ณผ์ ๊ณผ ์ ์ฌํฉ๋๋ค.
์ ์ฝ๋์์๋ if๋ฌธ์ ํตํด string์ธ์ง ๊ฒ์ฌํด์ฃผ๊ณ ์กฐ๊ฑด๋ฌธ ์ธ์ ๋ธ๋ก์์ ์๋์ผ๋ก numberํ์ธ์ง ๊ฒ์ฌํด์ค๋๋ค.
๋จ, typeof์ฐ์ฐ์๋ ์์ํ์ ์์๋ง ์ฌ์ฉํด์ผํฉ๋๋ค.
typeof a === 'object' ์ด๋ ๊ฒ ์ฌ์ฉํ๋๊ฒ์ด ๊ฐ๋ฅํ๊ธด ํ๋ฐ ๊ฐ์ฒด์ ์ธ๋ถ ํ๋กํผํฐ๊น์ง ํ์
์ด ์ขํ์ง์ง๋ ์๊ธฐ ๋๋ฌธ์
typeof ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ํ์ ๊ฐ๋๋ ๊ถ์ฅ๋์ง ์์ต๋๋ค.
๐ ๏ธ Narrowing์ ๊ธฐ์
๊ทธ๋ฐ๋ฐ ์ค์ ๋ก Narrowing์ ํ๋ค๋ณด๋ฉด ์ํ๋๋๋ก ๊น๋ํ๊ฒ Narrowing๋๋ ๊ฒ์ด ์๋
์๊ฐ์น๋ ๋ชปํ ์๋ฌ๊ฐ ํฐ์ง๋ ๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค.
๊ทธ๋ฐ ์๋ฌ๋ค์ ๋ง์์ฃผ๋ ๋ช๊ฐ์ง ์คํฌ๋ค์ ํ์ธํด๋ด ์๋ค.
null ๊ณผ undefined๋ฅผ ๊ฑธ๋ฌ๋ด๊ธฐ
&&์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด ๊ฑธ๋ฌ๋ด๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
&&์ฐ์ฐ์์ ๊ฒฝ์ฐ falsyํ ๊ฐ์ด ์์ผ๋ฉด ๊ทธ ๊ฐ์ ๋ฐํํฉ๋๋ค.
๊ทธ ๊ฒฐ๊ณผ ์กฐ๊ฑด๋ฌธ์์์ ์ฌ์ฉ๋ ๊ฒฝ์ฐ falsy๊ฐ์ด ์๋ค๋ฉด ์กฐ๊ฑด๋ฌธ ์คํ์ด ๋์ง ์์ต๋๋ค.
function fn(a: string | null){
if(a && typeof a === 'string'){
console.log(a)
}
else{
return
}
}
์ ์ฝ๋์์ a๊ฐ ๋ง์ฝ null์ด๋ผ๋ฉด a && typeof a === 'string' falsyํ ๊ฐ์ด๋ฏ๋ก ์กฐ๊ฑด๋ฌธ์ด ์คํ๋์ง ์์์ ๊ฒ๋๋ค.
in๋ฌธ์ ์ฌ์ฉํ์ฌ Narrowing
๊ฐ์ฒด์์ ์ง์ง ๊ทธ ํค๊ฐ ์๋์ง ํ์ธํ๋ in๋ฌธ์ด ์์ต๋๋ค.
์ด๊ฒ์ผ๋ก ํน์ ํค๊ฐ ์๋ ๊ฐ์ฒด๋ง ์ขํ์ค ์๋ ์์ต๋๋ค.
type Fish = { swim : () => void }
type Bird = [ fly : () => void }
function fn( a: FIsh | Bird ){
if('swim' in a){
a.swim()
}
else{
a.fly()
}
}
intanceof๋ฅผ ์ด์ฉํ Narrowing
Date๋ Math๊ฐ์ ์์ฑ์ ํจ์์ ๊ฒฝ์ฐ ์ธ์คํด์ค๋ฅผ ๊ฒ์ฌํ์ฌ Narrowing ํ ์ ์์ต๋๋ค.
instanceof๋ ํด๋์ค๊ธฐ๋ฐ์ ํ์ ๋ง ๊ฒ์ฌํ๋ฏ๋ก ์ธํฐํ์ด์ค๋ type์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ ๋ง๋ก Class๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ๋ง๋ค์ด์ง ์์ฑ์ ํจ์๋ผ๋ฉด, ์์์๊น์ง๋ ๊ฒ์ฌํด ์ค๋๋ค.
function fn (a : Date | string){
if(a instanceof Date){
a.getFullYear()
}else{
a.toUpperCase()
}
}
โ ๏ธ never type
ํจ์๊ฐ ๊ฐ์ง ์ ์๋ ํ์ ์ค neverํ์ ์ด๋๊ฒ์ด ์์ต๋๋ค.
never๋ผ๋ ํ์ ์ ๋ฑ์ฅํ ์ ์๋ ์กฐ๊ฑด์ด ์๋๋ฐ์
- ๋ฐํ๊ฐ์ด ์์ด์ผํจ
- ํจ์๊ฐ ์คํ์ด ๋๋์ง ์์์ผํจ.
๋๋์ void๋ ๊ฒน์น๋ ๋๋์ธ๋ฐ ์ never๊ฐ ๋ฐ๋ก ์ฌ์ฉ๋๋์ง ์์๋ณด์๋ฉด,
๋ชจ๋ ํจ์๋ undefined๋ฅผ returnํฉ๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ void๋ ์ค์ undefined๋ฅผ ๋ฆฌํดํ๋ ๊ฒ์ธ๋ฐ,
never๋ ์ ๋ ๊ทธ๋ด์ผ์ด์๋ ํ์ ์ ๋๋ค.
const fn = () =>{
throw new Error()
} // never
function fn(a:string){
if(typeof a === 'string')
console.log(a)
}else{
console.log(a)
}
}
function fn(){
while(true){ ... }
}
์ ์์์์ ๋ณด์ด๋ฏ neverํ์ ์ ๋ด๊ฐ ์ง์ ์ง์ ํ ์ผ์ด ์์ง๋ง,
์ข ์ข ์ฝ๋๋ฅผ ์์๊ฐ์ด ์ด์ํ๊ฒ ์ ์์ ๋ ์ถํํฉ๋๋ค.
'Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [TypeScript] Type ์์ฝ (0) | 2025.07.06 |
|---|---|
| [Typescript] generic (0) | 2025.07.01 |
| [Typescript] keyof & typeof ์ฐ์ฐ์ (1) | 2025.07.01 |
| [Typescript] ๋ฐฐ์ด (0) | 2025.06.30 |
| [Typescript] Object (0) | 2025.06.14 |