TypeScript
Intro
JavaScript ๋์ฒด ์ธ์ด์ ํ๋๋ก์จ, JS์ ์์ํ์ฅ(superset)
ES6์ ์๋ก์ด ๊ธฐ๋ฅ๋ค์ ์ฌ์ฉํ๊ธฐ ์ํด, Babel๊ณผ ๊ฐ์ ๋ณ๋ ํธ๋์คํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํ์ง ์์๋ ES6์ ์๋ก์ด ๊ธฐ๋ฅ์ JS Engine(Node, browser...)์์ ์คํ๊ฐ๋ฅ
ECMA์ ํ์ค์ ๋ฐ๋ผ๊ฐ ์ ์๋ ์ข์ ์๋จ.
์ฅ์
์ ์ ํ์
JS๋ type์ ์ฌ์ ์ ํ์ ์ ์ง์ ํ์ง ์๋๋ค.
(๋ฐํ์์์ ์๋ฌ ๋ฐ์๊ฐ๋ฅ์ฑ ๋์)
TS๋ ์ ์ ํ์ ์ ์ง์ํ๋ฏ๋ก ์ปดํ์ผ ๋จ๊ณ์์ ์ค๋ฅ๋ฅผ ํฌ์ฐฉํ ์ ์๋ค.
๋๊ท๋ชจ Application ๊ฐ๋ฐ ์ฉ์ด (์ค์ ๋ฐฉ์ง๋ฅผ ํ๋๊น)
๊ฐ๋ฐ ๋๊ตฌ์ ์ง์
IDE(ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ) ํฌํจ ๋ค์ํ ๋๊ตฌ ์ง์
ํ์ ์ ๋ณด๋ฅผ ์ ๊ณตํจ์ผ๋ก์จ, ํ์ ์ฒดํฌ, ์๋์์ฑ๊ธฐ๋ฅ, code assist...๋ฑ์ ์ง์๋ฐ์ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค.
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ง์
class, interface, module๋ฑ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉฐ ์์ ๊ฐ์ฒด ์งํฅ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
ํฌ๋ก์ค ํ๋ซํผ ์ง์
JS๊ฐ ์ค์๋๋ ๋ชจ๋ ํ๋ซํผ์์ ์ฌ์ฉ๊ฐ๋ฅ
DOM์ ์ด
์ต์ ECMAScript ์ง์
JS๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ํธ๋ฆฌํ ์ฌ์ฉ
๊ฐ๋ฐํ๊ฒฝ
Node.js์ค์น
Node.js๊ฐ ์ค์น๋ ํ๊ฒฝ์์ TS๋ฅผ ๊ตฌ๋์ํฌ์์๋ค.
VSCode (ํน์ Webstorm)
ํ์ ์คํฌ๋ฆฝํธ ๊ธฐ๋ฅ ๊ธฐ๋ณธ ๋ด์ฅ๋์ด์์.
TSํ์ผ(.ts, .tsconfig.json,..)์ธ์ํจ.
์ฝ๋ ๊ฒ์ฌ, ๋น ๋ฅธ ์์ , ์คํ ๋ฐ ๋๋ฒ๊น ๋ฑ ๋ค์ํ ๊ธฐ๋ฅ ๋ฐ๋ก ์ฌ์ฉ๊ฐ๋ฅ.
๋จ ์ปดํ์ผ๋ฌ๋ ๋ณ๋ ์ค์น(์ ์ญ์ค์น ์งํ)
typescript ์ปดํ์ผ๋ฌ๋ node.js ํ๋ก๊ทธ๋จ
npm install typescript -g์ค์นํ, ์ปค๋งจ๋ ์ฐฝ์
tsc [ํ์ผ๋ช ].ts๋ผ๊ณ ํ๋ฉด ์ปดํ์ผ๋ ๊ฒฐ๊ณผ๋ฌผ์ด ๋์ด
ํ์ผ๊ฒฝ๋ก ๋ณด์ด๊ธฐ ์ค์ : [ํ์ผ] - [๊ธฐ๋ณธ์ค์ ] - ์ค์ - [Breadcrumbs: Enabled] ์ฒดํฌ
์ปดํ์ผ๋ฌ ์ต์ ์ค์
์ต์ ์ค์ ์์ด ์ปดํ์ผ์ ๊ฑฐ์น๋ฉด ES5 ํํ ์ฝ๋๋ก ์ปดํ์ผ์ด๋๋ค.
ํน์ ๋ฒ์ ์ผ๋ก ์ฌ์ฉํ๊ณ ์ถ์๋ ์ฌ์ฉํ๋ ์ต์ :
---targettsc hello.ts --target es6๊ธฐ๋ฅ์ ์ฝ๋ (์: Promise, Promise๋ es6 ๋ฒ์ ์ด์)
--lib์ปดํ์ผํ๋ฉด err: Promise๋ฅผ ์ฐพ์์ ์๋ค๋ error
Promise๋ฅผ ์ง์ํ๊ธฐ ์ํ ๋ณ๋์ polyfill์ด ํ์ํ๋ค.
tsc hello.ts --lib es5,es2015.promise,es2015.iterable,dom tsc hello ts --lib es2015,dom <!--์ถ์ฝํ-->dom: console ์ฌ์ฉํ๊ธฐ ์ํด ์ถ๊ฐ
module๊ด๋ จ ์ต์
--moduleutil.tsํ์ผ์์ ๋ชจ๋์ export ํด์ฃผ๊ณ , hello.tsํ์ผ์์ import๋ก ๋ชจ๋ ๊ฐ์ ธ์จ ๋ค์ compile์งํ
tsc hello.ts --lib es2015,dom์ปดํ์ผ ๊ฒฐ๊ณผ, import๊ฐ require๋ก ๋ณ๊ฒฝ๋ ๊ฒ์ ํ์ธํ ์ ์์.
์ฆ es5๋ ๊ธฐ๋ณธ์ ์ผ๋ก commonJS์์คํ ์ ๋ฐ๋ฅด๊ณ ์๋ค๋ ์ ์ ์ ์ ์๋ค.
๋ฐ๋ผ์ node๋ก jsํ์ผ์ ์คํ์ํค๋ฉด ์คํ์ด ๋๋ค.
target์ต์ ์ es6๋ก ์ค๋ณด๊ธฐtsc hello.ts --target es6 --lib es2015,dom์ปดํ์ผ ๊ฒฐ๊ณผ๊ฐ es6๋ฒ์ ์ด๊ธฐ ๋๋ฌธ์ arrow function๋ ๋ณด์ด๊ณ , import๊ฐ ์ฌ์ฉ๋ ๊ฒ์ด ํ์ธ๋๋ค.
๋จ, js๋ฅผ node.js๋ก ์คํ์ํค๋ คํ๋ฉด syntax error๋ฅผ ๋ฟ์ด๋ธ๋ค.
์? target์ es6์ง๋ง ๋ชจ๋ ์์คํ ์ commonJSํํ์ด๊ธฐ๋๋ฌธ
ํด๊ฒฐ:
module์ต์ ์ผ๋ก ํด๊ฒฐtsc hello.ts --target es6 --lib es2015,dom --module commonjs
ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ ๋๋ง๋ค, ์ด ์ต์ ์ ๋งค๋ฒ ํฐ๋ฏธ๋์ ์ ๋ ฅํด์ ์คํ์์ผ์ผํ ๊น? Nope!!!!!!
compile ์ค์ ํ ์ ์๋ ๋ณ๋ ํ์ผ์ด ์์(tsconfig.json)
compile ์ต์ ์ ์ง์ ํ ์ ์๊ณ command๋ช ๋ น์ ํตํด ์ด๋ค ์ต์ ๋ค์ด ์ ์ฉ๋์ด์๋์ง๋ ํ์ธํ ์ ์๋ค.
showConfigtsc hello.ts --target es6 --lib es2015,dom --module commonjs --showConfigconfiguration ํ์ผ์ ์ค์ ํด์ ๋งค๋ฒ ์ต์ ์ CLI์ ์ ๋ ฅํ์ง ์์๋ ๋๊ฒ๋ ํด๋ณด์.
TypeScript ์ปดํ์ผ๋ฌ ์ค์ ํ์ผ(tsconfig.json)
tsconfig.json (์ค์ ) ํ์ผ์ ํ๋ก์ ํธ์ ์ต์๋จ ์์น์ ์กด์ฌํ๊ฒ๋จ
์ค์ ์ ์ฃผ๊ฒ๋๋ฉด ์ค์ ์ ์ํด ์ปดํ์ผ๋๋ฉฐ, jsonํํ
์ปดํ์ผํ๋๋ฐ ํ์ํ ๋ฃจํธํ์ผ๊ณผ ์ปดํ์ผ๋ฌ ์ต์ ์ค์
include:ํ์ ์คํฌ๋ฆฝํธ ์ปดํ์ผ๋ฌ์ ํฌํจ๋์ด์ง ํ์ผ ๋ชฉ๋ก๋คexclude: ์ ์ธ ๋ชฉ๋ก๋์ฒด๋ก typescript๋ nodejs๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ง๋ค.
npm init --y๋ก node project(pakage.json)๋ฅผ ๋ง๋ค๊ณ ์์ฑ๋๋ node modules๋ typescript ์ปดํ์ผ ๋์์์ ์ ์ธ
compilerOptions:์ปดํ์ผ๋ฌ ์ต์ ์
moduletarget๋ฑ๋ฑ
์ค์ ํ์๋ tsc๋ง ํธ์ถํด๋ ํ์ฌ dir๋ถํฐ ์์ dir chain์ ๋ฐ๋ผ์ tsconfig.jsonํ์ผ์ ๊ฒ์
์ค์ ๋ฐ ์ปดํ์ผ ํ, browser์์ ํ์ธํด๋ณด๋ฉด ์๋ณธํ์ผ์ ๋ณด์ด์ง ์๊ณ ์ปดํ์ผ ๋ jsํ์ผ๋ง ๋ณด์ธ๋ค. ๋ง์ฝ ์๋ณธํ์ผ(ts)์ ๋ณด๊ณ ์ถ์ ๊ฒฝ์ฐ ์ค์ ์ ์ถ๊ฐํ์.
"sourceMap": true
Last updated
Was this helpful?