๐Ÿ“’[TypeScript ๋งˆ์Šคํ„ฐ with Webpack & React] Step01.ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ์ดˆ

๊ถŒ์šฉ์ค€ยท2023๋…„ 12์›” 6์ผ
0
post-thumbnail

๊ธฐ์กด JS์— ์„ ํƒ์ ์œผ๋กœ ์ •์  ํƒ€์ž… ์„ ์–ธ(Optional Static Type Notation)์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œ๊ธฐ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

์„ ํƒ์  ์ •์  ํƒ€์ž… ์„ ์–ธ ํ‘œ๊ธฐ๋ฒ• (Optional Static Type Notation)

-> TS๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ณ€์ˆ˜์— ์ •์ ์œผ๋กœ ๋ฏธ๋ฆฌ ํƒ€์ž… ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

Type Script Primitive

Number
String
Boolean

Null
Undefined
void

any
Never
Unknown

1. Type Annotation


let ๋ณ€์ˆ˜๋ช…: ๋ณ€์ˆ˜ํ˜• = ๋ณ€์ˆ˜๊ฐ’;



let myString: string = "hello";

myString = 100; // error

myString = "bye"; // complete

2. Type Inference

TypeScript์—์„œ ๋ช…์‹œ์ ์ธ ํƒ€์ž… ํ‘œ๊ธฐ๊ฐ€ ์—†์„ ๋•Œ ํƒ€์ž… ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ ์ž๋™์œผ๋กœ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.


let food = "chicken";

food = 100; // error

food = "pizza"; // complete

3. any Type

any Type์€ ์–ด๋– ํ•œ ํƒ€์ž…๋„ ํ—ˆ์šฉํ•˜๋Š” ํƒ€์ž…์ด๋‹ค.

let thing: any = "hi";
thing = 1; // complete
thig = false; // complete
thing(); // complete

4. ์ง€์—ฐ๋œ ์ดˆ๊ธฐํ™” ๋ฐ ์•”๋ฌต์  Any (Type Annotation์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ)

const movies = ["Arrival", "The Thing", "Aliens", "Amadeus"];
let foundMovie; // 2. ์ด๋ฏธ ์•”๋ฌต์ ์œผ๋กœ Any Type์œผ๋กœ ์„ค์ •๋˜์–ด 

for(let movie of movies){
    if(movie === "Amadeus"){
        foundMovie = "Amadeus" // 1. foundMovie ๋ณ€์ˆ˜๊ฐ€ String Type์œผ๋กœ ์ถ”๋ก ํ• ๊ฑฐ๋ผ ์˜ˆ์ƒ๋˜์ง€๋งŒ 
    }
}

foundMoive(); // compile
foundMovie = 1; // compile

// 3. ์ปดํŒŒ์ผ์ด๋˜๋ฉฐ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. 
// TypScript์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์ธ ํƒ€์ž…์— ๊ด€ํ•œ ๊ธฐ๋Šฅ์„ ๋†“์น˜๊ฒŒ ๋œ๋‹ค.
goodExample
const movies = ["Arrival", "The Thing", "Aliens", "Amadeus"];
let foundMovie: string; // string ype์œผ๋กœ ์„ค์ •

for(let movie of movies){
    if(movie === "Amadeus"){
        foundMovie = "Amadeus" 
    }
}

foundMoive(); // error
foundMovie = 1; // error
profile
Brendan Eich, Jordan Walke, Evan You, ๊ถŒ์šฉ์ค€

0๊ฐœ์˜ ๋Œ“๊ธ€