[TypeScript] Narrowing ๊ณผ Assertion
- -
๐ฃ ๋ค์ด๊ฐ๊ธฐ ์ ์!
์ด๋ฒ์๋ Narrowing & Assertion์ ๋ํด์ ์ดํด๋ณผ ๊ฒ์ด๋ค. ํน์ ์ง์ง๋ ๊ฒ์๋ฌผ์์ ๋ค๋ค๋ union type์์ ์ค๋ฅ๊ฐ ๋ฌ๋ ๋ถ๋ถ๋ค์ ๊ธฐ์ต ํ๋์ง?!?! ๋๋ต ์๋์ ๊ฐ์ ์ฝ๋์๋๋ฐ, ์ฌ๊ธฐ์ ๋ฟ๋ฟํ ์๋ฌ๋ฅผ ํด๊ฒฐํ๋ ๋ฒ์ ์ง๊ธ๋ถํฐ ์์๊ฐ๋ณด๋๋ก ํ๊ฒ ๋ฐ!
let ๊ณ์ฐ: string | number
๊ณ์ฐ + 1; //error

union type์ ๋ํด์ ๊ธฐ์ต์ด ์ ์๋๋ค๋ฉด ์ฐธ๊ณ !
[TypeScript] union type · any · unknown
๐ฃ union type · any · unknown ๊ณตํต์ ์ด๋๊น?๋ค์ด๊ฐ๊ธฐ ์ ์ ์ด ์ธ๊ฐ์ง์ ๊ณตํต์ ์ ์๊ฐํด๋ณด๋ฉด! ์ด๋ค ํ์ ์ ์ข ๋ฅ๊ฐ ๋ค์ด์ฌ์ง ํ์คํ๊ฒ ๋ชจ๋ฅผ ๋ ์ฌ์ฉํ๋ค๋ ์ ์ ๋ค ์ ์๋ค. ํ๋ก์ ํธ ๋ด์์ ์ฌ์ฉํ
haileyham.tistory.com
๋ฐ๋ก ์ ๊ฒ์๋ฌผ์ void type์ ๋ํ ์ ๋ฆฌ ๊ธ!_!
[TypeScript] void type
๐ฃ ๋ค์ด๊ฐ๊ธฐ ์ ์์ด๋ฒ์๋ void type์ ๋ํด์ ์ ๋ฆฌํด๋ณด์ฅ!_! void ํ์ ์ ํจ์์ ํ์ ์ ์ง์ ํ ๋ ์ฌ์ฉํ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค. 'void'์ ๊ฒฝ์ฐ ๊ฐ์ด ์์์ ๋ํ๋ด๋๋ฐ, ์๋ฌด๊ฒ๋ ๋ฐํํ์ง ์๋(=return
haileyham.tistory.com
๐ฃ Type Narrowing
1. ๊ฐ๋
Type์ด ์์ง ํ๋๋ก ํ์ ๋์ง ์์์ ๊ฒฝ์ฐ ์ฌ์ฉ. ์ ํ ๊ฐ๋(๋ณดํธ) ๋๋ ์ ์ด ํ๋ฆ์ ์ฌ์ฉํ์ฌ ํน์ ๋ฒ์ ๋ด์์ ๋ณ์ ์ ํ์ ๊ตฌ์ฒดํํ๋ ํ๋ก์ธ์ค. ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก๋ typeof ์ฐ์ฐ์.
์ฝ๊ฒ ๋งํ๋ฉด if ๋ฌธ ๋ฑ์ผ๋ก ํ์ ์ ํ๋๋ก ์ ํด์ฃผ๋ ๊ฒ์ ๋งํจ.
if ๋ฌธ๊ณผ typeof ํค์๋๋ก ํ์ฌ ํ๋ผ๋ฏธํฐ ํ์ ์ ๊ฒ์ฌํ์ฌ ์ง์ .
* ์ฐธ๊ณ ๋ก typeof ์ธ์๋ ํ์ ์ ๊ตฌ์ฒดํ ์ํฌ ์ ์์ผ๋ฉด ๋ค ์ฌ์ฉ๊ฐ๋ฅ (in, instanceof)
2. ์ฝ๋๋ก ๋ ์ดํด๋ณด๊ธฐ
์๋ ์ฝ๋์์๋ if ๋ฌธ๊ณผ typeof ํค์๋๋ก ํ์ฌ ํ๋ผ๋ฏธํฐ ํ์ ์ ๊ฒ์ฌํ์ฌ ์ง์ .
function process(value: number | string) {
if (typeof value === "string") {
console.log(value.toUpperCase()); // ์ฌ๊ธฐ์ value๋ ๋ฌธ์์ด๋ก ์ฒ๋ฆฌ
} else {
console.log(value.toFixed(2)); // ์ฌ๊ธฐ์ value๋ ์ซ์๋ก ์ฒ๋ฆฌ
}
}
๐ฃ Type Assertion
1. ๊ฐ๋
ํ์ ๋ฎ์ด์ฐ๊ธฐ๋ผ๊ณ ์๊ฐ. TypeScript๋ณด๋ค ์ ํ์ ๋ํด ๋ ๋ง์ด ์๊ณ ์๋ ๊ฒฝ์ฐ TypeScript์ ๋ณ์์ ํน์ ์ ํ์ ์๋ฆด ์ ์์. as ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ฃ!
์ด๋ค ๋ณ์์ ํ์ ์ (์์) string ํ์ ์ผ๋ก ํ๊ณ ์ถ์ ๋ ์ฌ์ฉ.
์ผ์ชฝ์ ์๋ ๋ณ์๋ฅผ type์ผ๋ก ๋ฎ์ด์ฐ๊ธฐ (ex) x as number : x๋ณ์๋ฅผ number๋ก ๋ฎ๋ฎ)
๋ณ์๋ช as string // ์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋จ
์ด๋จ ๋ ์ฌ์ฉ?
1. Narrowing ํ ๋ ์ฌ์ฉ
2. ๋ฌด์จ ํ์ ์ด ๋ค์ด์ฌ์ง 100% ํ์คํ ๋ ์ฌ์ฉ
2. ์ฝ๋๋ก ๋ ์ดํด๋ณด๊ธฐ
์๋ ์ฝ๋๋ฅผ ์ดํด๋ณด๋ฉด 'value'๋ 'string'์ผ๋ก assertion ๋๋ฏ๋ก ํด๋น 'length' ์์ฑ์ ์ ๊ทผ ๊ฐ๋ฅ
let value: unknown = "Hello";
let strLength: number = (value as string).length; // value๊ฐ ๋ฌธ์์ด์์ ๋จ์ธ
---------------------------------
function ํจ์(x : number|string) {
let array: number[] = []
array[0] = x as number; // x๋ฅผ number๋ก ์ธ์
}
ํจ์(123)
---------------------------------
//์ฐธ๊ณ
(์ด๋ฆ as string) + 1; //ํ์ฌ๋ฌธ๋ฒ
<string>์ด๋ฆ + 1; //์๋ ๋ฌธ๋ฒ
---------------------------------
// ๊บพ์ ๊ดํธ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ ๋ ๋ค๋ฅธ ์
let someValue: any = "I am a string";
let strLength2: number = (<string>someValue).length;
๐ฃ ๊ทธ๋ ๋ค๋ฉด ์ฒ์์ ๊ถ๊ธ์ฆ์ ์ด๋ป๊ฒ ํด๊ฒฐ?
์ฒ์ union type ์๋ฌ๊ฐ ๋ฌ๋ ๊ฒ์ ํด๊ฒฐ๋ฟ ํ๋ ค๋ฉด
function ๋ดํจ์(x :number | string){
return (x as number) + 1
}
console.log( ๋ดํจ์(123) )
๐ฃ ๋ ๋์๊ฐ๋ณด๊ธฐ!_!
๋ง์ฝ์ array๊ฐ ๋ค์ด์ค๋๋ฐ [1, '2', 3] ์ด๋ฐ์์ผ๋ก ๋์ด์์ด์, ์ ๋ถ ์ซ์ ํ์ ์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์ถ๋ค๋ฉด?
function ๋ฟ๋ฟ(x: (number|string)[]) {
let ์ซ์: number[] = [];
x.forEach((a) => {
if (typeof a === 'string') {
์ซ์.push(parseFloat(a))
} else {
์ซ์.push(a)
}
})
return ์ซ์;
}
console.log(๋ฟ๋ฟ([123,'1']));
ํน์ฌ parseFloat์ ๋ํด ๊ถ๊ธํ๋ค๋ฉด
Number(), parseInt(), parseFloat() ์ฐจ์ด์ ๊ณผ ๊ณตํต์ [JavaScript]
Number()๋ฌธ์์ด์ ์๋ฅผ ์ซ์๋ก ๋ฐ๊ฟ์ค๋ค.๋ง์ฝ ์ธ์๋ฅผ ํ๋ณํ ํ ์ ์๋ค๋ฉด NaN์ ๋ฆฌํดํ๋ค.parseInt()๋ฌธ์์ด ์ธ์(์ซ์ + ๋ฌธ์)๋ฅผ ๋ฐ๋๋ค.๊ทธ ์ธ์๋ฅผ parse(ํด๋ถํ๋ค, ํ์ฌ๋ฌธ๋ฒ์ ๊ด๊ณ๋ฅผ ์ค๋ช ํ๋ค)ํ๋ค.int
velog.io
๐ฃ ์ฐธ๊ณ
๋ง์ฝ HTML ์์ narrowing์ ํ๋ค๋ฉด?
1. if
2. instanceof ์ฐ์ฐ์ (๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ)
3. as
4. ?. (optional chaining)
5. tsconfig.json ์ strict false ์ฒ๋ฆฌ(ํ์ ์ ์ธ ์ด์ ๊ฐ ์์)
* ์ฐธ๊ณ ๋ก optional chaining์ ๊ฒฝ์ฐ ์์ผ๋ฉด ์ถ๋ ฅ, ์๋๋ฉด undefined
// [1]
if (ํ์ดํ != null) {
ํ์ดํ.innerHTML = "๋ฐ๊ฐ์์ฉ"
}
// [2]
if (ํ์ดํ instanceof Element) {//HTML์ด Element๋ก ์ค๋ ๊ฒ์ธ์ง ํ์ธ > true๋ก ๋ณผ๋ก๋ฌธ ์คํ
// ์ถํ class์์ object๊ฐ class์ ์์, instance์ธ์ง๋ฅผ ํ์ธ
ํ์ดํ.innerHTML = "๋ฐ๊ฐ์์ฉ"
}
// [3]
let ํ์ดํ1 = document.querySelector("#title123์๋ชป์
๋ ฅ") as Element;
// as ํค์๋ ์ฌ์ฉํด์ id ๊ฐ ์
๋ ฅ ์๋ชปํด๋ Element๋ก ํ์
์ง์
// [4]
if (ํ์ดํ?.innerHTML != undefined) {
ํ์ดํ.innerHTML = "๋ฐ๊ฐ์ก"
}
// ์ค๋ธ์ ํธ์ ๋ถ์ด๋ ?.
//1. ํ์ดํ์ innerHTML์ด ์์ผ๋ฉด ์ถ๋ ฅ
//2. ์์ผ๋ฉด undefined
๋ง์ ๋ด๊ฐ as ์ธ ๋๋ ์ค์ง ์ค๋ฅ๋ฅผ ์์ ํด๊ฒฐํ ๋...@_@
[TypeScript] ๊ฐ์ฒด์ ํ์ ๊ณผ readonly
๐ฃ ๋ค์ด๊ฐ๊ธฐ ์ ์!๊ฐ์ฒด์๋ ํ์ ์ด ์กด์ฌํ๋ค!_! ๋น์ฐํ ์ฌ์ค..! ๋๋ฅ! ๊ทธ๋ฌ๋ฉด ์ด๋ฒ์๋ ๊ฐ์ฒด์ ํ์ ๊ณผ readonly์ ๋ํด์ ์ดํด๋ณด์!_! ๊ฐ๋จํ๋ ์งง์ ๊ธ์ด ๋๊ฒ ๋ฐ! ํน์ ์ง๋ ๊ฒ์๋ฌผ์ด ๊ถ๊ธํ๋ค๋ฉด
haileyham.tistory.com
'FrontEnd > TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [TypeScript] ํ์ ๋ณ์ type alias (0) | 2024.06.20 |
|---|---|
| [TypeScript] ๊ฐ์ฒด์ ํ์ ๊ณผ readonly (0) | 2024.06.20 |
| [TypeScript] void type ยท ํจ์์ ํ์ (1) | 2024.06.20 |
| [TypeScript] union type ยท any ยท unknown (0) | 2024.06.20 |
| [TypeScript] ํ์ ์คํฌ๋ฆฝํธ (0) | 2024.06.18 |
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค
