
function ํจ์(x :number | string){
return x + 1
}
์ด๋ ๊ฒ ํจ์๋ฅผ ์ง์ ํด์ฃผ๋ฉด ์๋ฌ๊ฐ ๋๋ค.

ํ์
์คํฌ๋ฆฝํธ๋ ์ฌ๋ฌ ๊ฐ์ ํ์
์ ์ด์ฉํ ๋
ํ์
์ ํ์ ์ง์ด์ฃผ์ง ์์ผ๋ฉด ๊น๋ค๋ก์ด ํ์
์คํฌ๋ฆฝํธ๋ ์ค๋ฅ๋ฅผ ๋ด๊ธฐ ๋๋ฌธ์,
narrowing์ ๊ผญ ํด์ฃผ์ด์ผ ํ๋ค.
์ด๋ด ๋ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ด narrowing์ด๋ assertion์ ์ด์ฉํ๋ ๊ฑด๋ฐ,
assertion์ ๋ํ ๋ด์ฉ์ ์ ๋ฒ ์์ ๊ฒ์๊ธ์ ์์ผ๋
๊ธฐ๋ณธ์ ์ narrowing์ด๋ ์ ๋ฆฌํด๋ณด๋๋ก ํ์ฅ
If ๋ฌธ
function ํจ์(x: number | string){
if (typeof x === 'string'){
return x + '1';
} else {
return x + 1;
}
}
์ ์ผ ์ง๊ด์ ์ด๊ณ ์ฌ์ด ๋ฐฉ๋ฒ์ด๋น
else {} ์ด๊ฑฐ ์์ผ๋ฉด ์๋ฌ๊ฐ ๋๋ค.
๋ ์ด๊ฑฐ๋ return ์์ด~ ํ๊ณ ์ค๋ฅ ๋ ๊น๋ด ์๋ ค์ฃผ๋ ๊ฑด๋ฐ,
tsconfig.js ์์ "noImplicitReturns": false, ์ด๊ฑธ ๋ฃ์ด์ฃผ๋ฉด ์ค๋ฅ๊ฐ ์๋๋ค.
๊ทธ๋๋ ์๊ฒฉํ๊ฒ ์ฐ๋ ๊ฒ ์ข์ผ๋ else๋ฅผ ๋นผ๋จน์ง ๋งใน๋๋ก ํ์
in ์ฐ์ฐ์
in ์ฐ์ฐ์๋ก object ์๋ฃ narrowingํ๋ ๋ฐฉ๋ฒ
object์์ ์๋ก ๋ค๋ฅธ ์ ๋ํฌํ ์์ฑ๋ค์ ๊ฐ์ง๊ณ ์๋ค๋ฉด
if (์ด ํ๋ผ๋ฏธํฐ๊ฐ a๋ผ๋ ์์ฑ์ ์์ ๊ฐ์ง๊ณ ์๋) ์ด๋ฐ if๋ฌธ์ ์จ๋ narrowing์ด ๊ฐ๋ฅํ๋ค.
if (ํค๊ฐ in object์๋ฃํ) ์ด๋ ๊ฒ ์จ์ฃผ๋ฉด ๋จ.
type Fish = { swim: string };
type Bird = { fly: string };
์ด๋ด ๋ typeof ๋ฅผ ์ธ ์๊ฐ ์๋ค.
typeof ๋ ๋ง๊ทธ๋๋ก string, number, boolean ๊ฐ์ ํ์
๋ง ๊ฐ์ ธ์ฌ ์ ์๊ธฐ ๋๋ฌธ์,
๋ ๋ค๋ฅธ ์์ฑ์ ๊ฐ์ง ์ค๋ธ์ ํธ๋ฅผ ๋น๊ตํ ์๊ฐ ์์.
function ํจ์(animal: Fish | Bird){
if ('swim' in animal) {
return animal.swim
}
return animal.fly
}
๊ทธ๋ด ๋ ์์ฒ๋ผ in์ ์ธ ์๊ฐ ์๋ค.
์๋ก ๋ฐฐํ์ ์ธ ์์ฑ์ ์ง๋ ๋ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
Discriminated Union
์ ํด์ง literal type
type Car = {
wheel : '4๊ฐ',
color : string
}
type Bike = {
wheel : '2๊ฐ',
color : string
}
type Transport = Car | Bike
ํ์ Car, Bike๋ ๋ ๋ค wheel๋ผ๋ ๋์ผํ ํค๋ฅผ ๊ฐ์ง๋ง ํค์ ๊ฐ์ ๊ฐ '4๊ฐ', '2๊ฐ'๋ก ๋ค๋ฅธ ๊ฐ์ ๊ฐ๋๋ค.
function isCar(x : Transport){
if (x.wheel === '4๊ฐ'){
console.log('the car is ' + x.color)
} else {
console.log('the bike is ' + x.color)
}
}
ํจ์ isCar ์ธ์๋ Transport ํ์ ์ ๊ฐ๋๋ค.
- ์ธ์๋ก ๋ค์ด์จ ๊ฐ์ ํค์ธ wheel ๊ฐ์ด 4๊ฐ์ธ ๊ฒฝ์ฐ, ์ธ์๋ก Car๊ฐ ๋ค์ด์จ ๊ฒ์ด๋ฏ๋ก x์๋ฆฌ์ Car์ color๊ฐ์ ํ๋ค
- ์ธ์๋ก ๋ค์ด์จ ๊ฐ์ ํค์ธ wheel ๊ฐ์ด 2๊ฐ์ธ ๊ฒฝ์ฐ, ์ธ์๋ก Bike๊ฐ ๋ค์ด์จ ๊ฒ์ด๋ฏ๋ก Bike์ color ๊ฐ์ ์ถ๋ ฅํ๋ค.
?
interface Shape {
kind: "circle" | "square";
radius?: number;
sideLength?: number;
}
์ด๋ฐ interface๊ฐ ์๋ค ์น ๋,
๋ํ์ ์ข
๋ฅ๊ฐ ์์ผ์ง, ์ฌ๊ฐํ์ผ์ง ๋ชจ๋ฅด๋ฏ๋ก radius๋ sideLength์ ๊ฐ์ ? ๋ก ๋์๋ค.
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
}
๋ง์ฝ ๋ฉด์ ์ ๊ตฌํ๋ ํจ์๋ฅผ ์ด๋ ๊ฒ ๋ํ๋ธ๋ค๋ฉด ๋น์ฐํ ์ค๋ฅ๊ฐ ๋๋ค
์๋๋ฉด radius๊ฐ ์๋์ง ์๋์ง๋ ๋ชจ๋ฅด๋๋ฐ ๋ค์ง๊ณ ์ง ์ฐ๋ผ๊ณ ํ๋ ์๋ฏผํน ํ์
์คํฌ๋ฆฝํธ๋ ์๋๋ค๊ณ ๋งํ๋ ๊ฑฐ๋ค

function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius ** 2;
}
}
๊ทธ๋ ๋ค๊ณ if๋ฅผ ์ฌ์ฉํด์ ์ kind๋ ์์ด์ผ! ๋ผ๊ณ ํ๋ค ํด๋ดค์ ์๋๋ค
์ด์ฐจํผ radius ๋ชจ๋ฅด๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง๋๊น..
function getArea(shape: Shape) {
if (shape.kind === "circle") {
return Math.PI * shape.radius! ** 2;
}
}
radius๋ ์์ด! ์๋ค๊ตฌ! ํ๊ณ ๋งํด๋์ผ๋ฉด ์ค๋ฅ์ ์ธ๋ ๋ฉดํ ์ ์๋ค
ํ์ง๋ง.. radius๋ฅผ ์ ์จ๋ฒ๋ฆฌ๋ฉด.. ๋ ์ค๋ฅ๊ฐ ๋ ๊ฒ์ด์ผ
์ด๋ ํ์ ์ง์ ๋ฐฉ์ ์์ฒด๋ฅผ ๋ฐ๊พธ์ด ์ฃผ๋ฉด ๋๋ค.
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
sideLength: number;
}
type Shape = Circle | Square;
์ด๋ ๊ฒ ๋๋ฉด Shape ๋ผ๋ ํ์
์ ๊ณตํต์ ์ผ๋ก kind ๋ผ๋ ์์ฑ์ ๊ฐ์ง๊ณ , ๊ฐ๊ฐ ๊ณ ์ ์ ํ์์ ์ธ ์์ฑ๋ ๊ฐ์ง๊ฒ ๋๋ ๊ฒ์ด๋ค.
function getArea(shape: Shape) {
return Math.PI * shape.radius ** 2;
}

์ด๋ฌ๋ฉด ๋ณด๋ค ์ ํํ ์ค๋ฅ๋ฅผ ์๋ ค์ฃผ๊ฒ ๋๋ค.
์ด๋ if๋ฌธ์ ์ฌ์ฉํด์ฃผ๋ฉด ์ค๋ฅ์์ด ts๊ฐ ํ์
์ ์ ๊ฐ์งํ๊ฒ ๋๋ค.
์ด๋ ๊ฒ ์ฝ๋๋ฅผ ๋ ์ง๊ด์ ์ผ๋ก ์์ฑํ ์ ์๊ณ ,
๊ณตํต์ ์ธ property๋ฅผ ๊ฐ๊ฒ ๋ง๋ค์ด ๊ตฌ๋ถ์ด ์ฝ๋๋ก ํ๋ค.
instanceof
x instanceof Foo ๋ Foo ๋ผ๋ ํ๋กํ ํ์ ์ฒด์ธ ์์ x๋ผ๋ ์ธ์คํด์ค๊ฐ ์๋ ํ์ธํ๋ ๊ฒ์ด๋ค.
- ํ๋กํ ํ์ ๊ณผ์ธ์คํด์ค : https://developer.mozilla.org/ko/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
function logValue(x: Date | string) {
if (x instanceof Date) {
console.log(x.toUTCString());
} else {
console.log(x.toUpperCase());
}
}
null & undefined ์ฒดํฌํ๊ธฐ
if (์ ๋ณ์๊ฐ undefined์ผ ๊ฒฝ์ฐ) ์ด๋ ๊ฒ ํด์ค~
์ด๋ค๋ณด๋ฉด ์ด๋ฐ ๊ฒฝ์ฐ๋ฅผ ๋ง์ด ๋ง์ดํ๊ฒ ๋๋ค.
์ด๋ฐ ์ํฉ์ ๋ฐฉ์ดํด ์์ ํ๊ฒ ํ๊ณ ๋์ด๊ฐ๋๊ฒ ์ข๊ธฐ ๋๋ฌธ์ด๋ค.
function ํจ์(a : string | undefined){
if (typeof a === 'string') {
} else {
}
}
๋น์ฐํ ์ด๋ ๊ฒ ์ฐ๋ฉด ๋๋ค.
๊ทธ๋ฐ๋ฐ ์ด ์ฝ๋๋ฅผ ํ ์ค๋ก ์ค์ผ ์๊ฐ ์์
function ํจ์(a : string | undefined){
if (a && typeof a === 'string') { ...
๊ทธ๋ฌ๋๊น,
if (๋ณ์ && typeof strs === "string") {}
์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด ๋ณ์๊ฐ undefined๋ผ๋ฉด undefined๊ฐ ๋จ์์ if๋ฌธ์ด ์คํ๋์ง ์๊ณ ,
(if๋ฌธ ์กฐ๊ฑด์์์ falsy ๊ฐ์ด ๋จ์ผ๋ฉด if๋ฌธ ์คํXXX)
๋ณ์๊ฐ string ํ์
์ด๋ฉด if๋ฌธ์ด ์คํ๋๋ค.
์ฆ null, undefined๋ฅผ ์ฝ๊ฒ ๊ฑฐ๋ฅผ ์๊ฐ ์๋ ๊ฒ์ด๋ค.
&&
์๋ && ์ด๊ฑด ์กฐ๊ฑด์ 2๊ฐ๊ฐ ์ฐธ์ด๋ฉด ์ ๋ถ ์ฐธ์ผ๋ก ํ์ ํด์ฃผ์ธ์~ ๋ผ๋ ๋ ผ๋ฆฌ์ฐ์ฐ์์ธ๋ฐ ์ฌ๋ฌ ๊ฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์ด์ํ ํ์์ด ์๋ค.
- && ๊ธฐํธ๋ก ๋น๊ตํ ๋ true์ false๋ฅผ ๋ฃ๋๊ฒ ์๋๋ผ ์๋ฃํ์ ๋ฃ์ผ๋ฉด,
&& ์ฌ์ด์์ ์ฒ์ ๋ฑ์ฅํ๋ falsy ๊ฐ์ ์ฐพ์์ฃผ๊ณ , ๊ทธ๊ฒ ์๋๋ฉด ๋ง์ง๋ง ๊ฐ์ ๋จ๊ฒจ์ค - falsy ๊ฐ์ false์ ์ ์ฌํ ๊ธฐ๋ฅ์ ํ๋ null, undefined, NaN ์ด๋ฐ ๊ฐ๋ค์ ์๋ฏธ
1 && null && 3 // null์ด ๋จ์
undefined && '์๋
' && 100 // undefined ๋จ์
๐๐ป ์ฐธ๊ณ
'TIL > Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TypeScript + React] ์นด์นด์ค ๋งต ์ด์ฉํ๊ธฐ (0) | 2023.05.20 |
---|---|
[TIL] TypeScript ๋ก ๋คํฌ๋ชจ๋ ๊ตฌํํ๊ธฐ ๐ / ์ด๋ฏธ์ง ์ฝ์ ๐ (1) | 2023.05.20 |
[TIL] Class + TS (1) | 2023.05.20 |
Typescript ์์ ์ ํจ๊ป ์ต์ํด์ง๊ธฐ : part 1 (1) | 2023.05.19 |
Typescript ๊ธฐ์ด ๋ฌธ๋ฒ ์ ๋ฆฌ (0) | 2023.05.19 |