[TypeScript] Hello TypeScript! with Deno๐Ÿฆ•

2020๋…„ 08์›” 31์ผ

TOC

โค

TypeScript?

TypeScript์— ๋Œ€ํ•ด์„œ ์ฒ˜์Œ ๊ด€์‹ฌ์„ ๊ฐ€์ง€๊ฒŒ ๋œ ๊ฒƒ์€ React๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ์ค‘๊ฐ„ ์ค‘๊ฐ„ Type์— ๋Œ€ํ•ด ์ œ๋Œ€๋กœ ์ฒดํฌ๋ฅผ ์•ˆํ•˜๊ณ  ๊ทธ๋ƒฅ ์‚ฌ์šฉ์„ ํ•˜๋‹ค ๋Ÿฐํƒ€์ž„์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์œผ๋กœ ๋ณด๊ณ  ์ˆ˜์ •์„ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ์›์ฒœ ์ฐจ๋‹จ ํ•  ์ˆ˜ ์žˆ๋Š” JavaScript ์˜ SuperSet ์–ธ์–ด์ธ TypeScript๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜๊ณ  ๊ด€์‹ฌ์„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค.

TypeScript๊ฐ€ ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฌธ์ œ๋ฅผ ์›์ฒœ ์ฐจ๋‹จ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ด์œ ๋Š” JS์™€ ๋‹ฌ๋ฆฌ ์ปดํŒŒ์ผ ๊ณผ์ •์—์„œ TypeChecker๋ฅผ ํ†ตํ•ด ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆซ์ž์™€ ๋ฌธ์ž๋ฅผ ๊ณฑํ•˜๊ณ  ์žˆ๋Š”์ง€ ์™€ ๊ฐ™์€ ํ–‰์œ„๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ  ๋ฏธ๋ฆฌ ๊ฒฝ๊ณ ๋ฅผ ํ•จ์œผ๋กœ์จ ๊ฐœ๋ฐœ์ž๋Š” ์‹ค์ˆ˜๋ฅผ ์ค„์ด๊ณ  ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ •์ƒ์ ์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ๋  ์ˆ˜ ์žˆ๋‹ค.

์ฒ˜์Œ TypeScript์— ๋Œ€ํ•ด ์•Œ์•˜์„ ๋•Œ ์ปดํŒŒ์ผ์„ ํ•ด ์ค€๋‹ค๊ณ ?? JavaScript๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด์ธ๋ฐ? ํ•˜๋ฉด์„œ ์˜๋ฌธ์ ์ด ์ƒ๊ฒผ๋‹ค.

์ด๋•Œ TypeScript๋Š” ์œ„์—์„œ ๋งํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด JavaScript์˜ SuperSet์œผ๋กœ TypeScript๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ JavaScript๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

๊ทธ ๊ณผ์ •์—์„œ ํƒ€์ž…์— ๋Œ€ํ•ด ์ฒดํฌ๋ฅผ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋Ÿฐํƒ€์ž„์ด ์•„๋‹Œ ์ปดํŒŒ์ผ ์‹œ์ ์—์„œ ํƒ€์ž…์— ๋Œ€ํ•ด ์ž˜๋ชป๋œ ๋ถ€๋ถ„์„ ์•Œ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

TS:
1: ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค -> ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ AST
2: ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ๊ฐ€ AST๋ฅผ ํ™•์ธ
3: ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ AST -> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค
JS:
4: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค -> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ AST
5: AST -> ๋ฐ”์ดํŠธ์ฝ”๋“œ
6: ๋Ÿฐํƒ€์ž„์ด ๋ฐ”์ดํŠธ์ฝ”๋“œ๋ฅผ ํ‰๊ฐ€

์œ„์™€ ๊ฐ™์€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น˜๋ฉด์„œ TypeScript๊ฐ€ JavaScript ์†Œ์Šค๋กœ ์ƒ์„ฑํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ € ๊ธฐํƒ€ ์—”์ง„์— ์˜ํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰์ด ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Node.js ์—์„œ TypeScript๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” typescript tslint @types/node ๋“ฑ์˜ ์ถ”๊ฐ€์ ์ธ ๋ชจ๋“ˆ์ด ์š”๊ตฌ๊ฐ€ ๋˜๋Š”๋ฐ deno ๋ผ๋Š” ์ƒˆ๋กœ์šด ์—”์ง„์„ ์‚ฌ์šฉํ•ด์„œ ์ถ”๊ฐ€์ ์ธ ๋ชจ๋“ˆ์ด ์—†์ด ์ž‘๋™์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋œ๋‹ค.

Deno?

Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

Deno์˜ ๊ณต์‹ ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ์œ„์™€ ๊ฐ™์ด ์„ค๋ช…์„ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Deno๋Š” Node.js์˜ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ธฐ์กด Node.JS๋ฅผ ๋งŒ๋“ค์—ˆ๋˜ ์ƒํ™ฉ๊ณผ ์ง€๊ธˆ์˜ JavaScript์˜ ์ƒํƒœ๊ณ„๊ฐ€ ๋ณ€ํ™”๋จ์œผ๋กœ์จ ๊ทผ๋ณธ์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋˜ ๋‹จ์ ๋“ค์„ ๊ฐœ์„ ํ•œ ํ”„๋กœ์ ํŠธ ์ด๋‹ค.

๊ธฐ์กด Node.js์™€์˜ ์ฐจ์ด์ ์„ ์•„๋ž˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค.

Node.js Deno
Engine V8 V8
Language C++, javascript rust, typescript
Package manager NPM URLs or file paths
Modules import CommonJs ES Modules
Security full access explicit permissions
TypeScript Support not built in built in
async actions return value return undefined or promise return promise

Node.js์™€ Deno๋Š” ์œ„์™€ ๊ฐ™์€ ์ฐจ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„์ง ๊ฐœ๋ฐœ์ด Node.js ์™€ ๋‹ฌ๋ฆฌ ์ดˆ๊ธฐ์ธ ์ ์„ ๊ณ ๋ คํ•˜์—ฌ์„œ ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

๊ธฐ์กด Node.js ๋Š” TypeScript๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉ ํ•ด์•ผ ํ•˜๊ณ  ๋ณต์žกํ•œ ์„ค์ •์ด ํ•„์š”ํ•˜์ง€๋งŒ Deno๋Š” ์ž์ฒด์ ์œผ๋กœ TypeScript๋ฅผ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒˆ ๊ธฐํšŒ์— ๊ฐ™์ด ๊ณต๋ถ€ํ•˜๊ณ ์ž ์ด๋ ‡๊ฒŒ ์ •๋ฆฌ ๊ธ€์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

Hello TypeScript! with Deno๐Ÿฆ•

์ด์ œ ํ•œ๋ฒˆ Deno๋ฅผ ์„ค์น˜ํ•˜์—ฌ์„œ Hello TypeScript! - with ๐Ÿฆ• ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅ ํ•ด๋ณธ๋‹ค!

Installation

Deno works on macOS, Linux, and Windows. Deno is a single binary executable. It has no external dependencies.

Using Shell (macOS and Linux):

curl -fsSL https://deno.land/x/install/install.sh | sh

Using PowerShell (Windows):

iwr https://deno.land/x/install/install.ps1 -useb | iex

Using Scoop (Windows):

scoop install deno

Using Chocolatey (Windows):

choco install deno

Using Homebrew (macOS):

brew install deno

Using Cargo (Windows, macOS, Linux):

cargo install deno

Deno์—์„œ ์ œ๊ณตํ•˜๋Š” ์„ค์น˜ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์„ค์น˜๋ฅผ ์ง„ํ–‰ ํ•œ๋‹ค.

์ด์ œ hello.ts ๋ผ๋Š” ๊ฐ„๋‹จํ•œ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ ์ž‘์„ฑ์„ ํ•ด๋ณธ๋‹ค.

console.log("Hello TypeScript! - with ๐Ÿฆ•")

๊ทธ๋ฆฌ๊ณ  deno run hello.ts ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฌผ์„ ํ™•์ธ ํ•ด๋ณธ๋‹ค!

์ด์ œ TypeScript์˜ ์žฅ์ ์ธ TypeChecker๊ฐ€ ๋™์ž‘ํ•˜๋Š” ์ง€ ํ™•์ธํ•œ๋‹ค.

const num = 10
const word = "test"

console.log(num * word)

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑ๋œ ํŒŒ์ผ์„ ๋ณด๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐ”๋กœ ์ถœ๋ ฅ ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

image-20200901025605633

์ด๋ฒˆ์—๋Š” annotation๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์˜ Type๋ฅผ ์ง€์ • ํ•˜์˜€์„ ๋•Œ ์–ด๋–ค ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธ ํ•œ๋‹ค.

const num: number = 10
const word: number = "test"

console.log(num * word)

image-20200901025947806

์œ„์™€ ๊ฐ™์ด ํƒ€์ž…์— ๋Œ€ํ•˜์—ฌ ์ •์˜๋ฅผ ํ†ตํ•ด ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์— ๋Œ€ํ•œ ์‹ค์ˆ˜๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.


ํ•œ๋ฒˆ ๊ฐ„๋‹จํ•˜๊ฒŒ TypeScript์™€ Deno์— ๋Œ€ํ•ด์„œ ๊ธ€์„ ํ•ด๋ณด์•˜๋Š”๋ฐ Deno์˜ ์žฅ์ ๊ณผ ์™œ TypeScript๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ์•„๊ฐ€๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋‹ค.

Buy me a coffeeBuy me a coffee
Written by

@JaeSeoKim

๋ณด์•ˆ๊ณผ ๊ฐœ๋ฐœ์„ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ ์ž…๋‹ˆ๋‹ค~!