๐Ÿ’ป TypeScript๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€์š”? ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์‹ค์šฉ์ ์ธ ๊ฐ€์ด๋“œ ๐Ÿ› 

3201 ๋‹จ์–ด devreltypescript

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ž€?



TypeScript์€ JavaScript์˜ ๋ชจ๋“  ์œ ์—ฐ์„ฑ๊ณผ ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋Šฅ ์œ„์— a type system์„ ์ œ๊ณตํ•˜๋Š” Microsoft์—์„œ ๋งŒ๋“  ์ธ๊ธฐ ์žˆ๋Š” JavaScript ์ƒ์œ„ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.

์ด ์–ธ์–ด๋Š” ์˜คํ”ˆ ์†Œ์Šค ํ”„๋กœ์ ํŠธlicensed under the Apache License 2.0๋กœ ๊ตฌ์ถ•๋˜์—ˆ์œผ๋ฉฐ ๋งค์šฐ ํ™œ๋ฐœํ•˜๊ณ  ํ™œ๊ธฐ์ฐฌ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ๋ณด์œ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์ฒ˜์Œ ์‹œ์ž‘๋œ ์ดํ›„๋กœ ํฌ๊ฒŒ ๋„์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค.

TypeScript ์„ค์น˜



TypeScript๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ๋ชจ๋“  ์˜ˆ์ œ๋ฅผ ์‹œ๋„ํ•˜๋ ค๋ฉด ์ปดํ“จํ„ฐ์— TypeScript ๋ณ€ํ™˜๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜(์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ ๋‹จ๋ฝ ์ฐธ์กฐ) official online playground ๋˜๋Š” ์›ํ•˜๋Š” ๋‹ค๋ฅธ ๋ณ€ํ™˜๊ธฐonline solution๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋กœ์ปฌ์—์„œ ์˜ˆ์ œ๋ฅผ ์‹œ๋„ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ Node.js์—์„œ ์‹คํ–‰๋˜๋Š” ๋ช…๋ น์ค„ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์‹œ์Šคํ…œ์—์„œ install Node.js and npm ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ Node.js ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  TypeScript ๋ณ€ํ™˜๊ธฐ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# Create a new directory for your project
mkdir typescript-intro

# Make your project directory the current directory
cd typescript-intro

# Initialize a new Node.js project
npm init -y

# Install the TypeScript compiler
npm i typescript


์ด๊ฒƒ์€ ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์— tsc (TypeScript Compiler) ๋ช…๋ น์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•„๋ž˜์— index.ts๋ผ๋Š” TypeScript ํŒŒ์ผ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

console.log(1);


๊ทธ๋Ÿฐ ๋‹ค์Œ ํŠธ๋žœ์ŠคํŒŒ์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JavaScript๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

# transpiling index.ts to index.js
npx tsc index.ts


๊ทธ๋Ÿฌ๋ฉด TypeScript ํŒŒ์ผ๊ณผ ์™„์ „ํžˆ ๋™์ผํ•œ ์ฝ”๋“œ๋กœ index.js๋ผ๋Š” ์ƒˆ ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. node ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ƒˆ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค.

# this will output 1
node index.js


๋ณ€ํ™˜๊ธฐ๊ฐ€ JavaScript ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์›๋ณธ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ ์™ธ์— ๋‹ค๋ฅธ ์ž‘์—…์€ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ์ด ๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด TypeScript ์„ค์น˜๊ฐ€ ์–‘ํ˜ธํ•œ ์ƒํƒœ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ์ฒ˜๋ฆฌํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Note: TypeScript versions can have substantial differences even though they get released as minor revisions. It's common to bump into transpilation problems after a minor version update. For that reason, it is better to install TypeScript locally in your project and execute it using npx when needed instead of relying on a global TypeScript installation.



Read more...

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ