
npm init -y
๐ ํ๋ก์ ํธ๋ฅผ ์์ํ๋ฉด package.json์ด ์์ฑ๋จ
๐ ์ญํ
ํ๋ก์ ํธ ์ ๋ณด ์ ์ฅ
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ด๋ฆฌ
๐ ์ฝ๊ฒ ๋งํ๋ฉด ํ๋ก์ ํธ ์ค๋ช ์
npm i @types/node
๐ Node.js ๊ธฐ๋ฅ์ TypeScript๊ฐ ์ดํดํ๋๋ก ๋์์ค
console.log(process.version);
๐ ์ด ์ฝ๋์์ process๋ฅผ ์ธ์ํ๋ ค๋ฉด ํ์ํจ
tsc
๐ TypeScript๋ฅผ JavaScript๋ก ๋ณํ
โ ์คํ ํ๋ฆ
index.ts โ (tsc) โ index.js โ node ์คํ
๐ Node.js๋ TypeScript๋ฅผ ์ง์ ์คํ ๋ชปํจ โ
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"rootDir": "./src",
"outDir": "./dist",
"strict": true,
"moduleDetection": "force"
},
"include": ["src"]
}
target โ JS ๋ฒ์
module โ import/export ๋ฐฉ์
rootDir โ TS ์์น
outDir โ JS ๊ฒฐ๊ณผ ์์น
strict โ ์๊ฒฉ ๋ชจ๋
npx tsx src/index.ts
๐ ์ปดํ์ผ ์์ด ๋ฐ๋ก ์คํ ๊ฐ๋ฅ
"ts-node": {
"esm": true
}
package.json
"type": "module"
tsconfig.json
"module": "esnext"
๐ ์ด 3๊ฐ๊ฐ ์ธํธ
"strictNullChecks": false
๐ null / undefined๋ฅผ ํ์ฉํ ์ง ๊ฒฐ์
let name: string = "minji";
name = null; // ์๋ฌ ์ ๋จ
๐ ์คํ ์ค ํฐ์ง ๊ฐ๋ฅ
"strictNullChecks": true
let name: string = "minji";
name = null; // ์๋ฌ ๋ฐ์
๐ ๋ฏธ๋ฆฌ ๋ฐฉ์ง ๊ฐ๋ฅ
let name: string | null = null;
๐ ๋๋ถ๋ถ JS ์๋ฌ ์์ธ:
Cannot read property of null
undefined ์๋ฌ
๐ ์ด๊ฑธ ๋ฏธ๋ฆฌ ๋ง์์ค