๐ŸŸฆtypeScipt ๊ธฐ๋ณธ ์„ค์ •

yyยท2024๋…„ 1์›” 16์ผ
0

์žก๋™์‚ฐ์ด

๋ชฉ๋ก ๋ณด๊ธฐ
11/21

๐ŸŸฆ 0. npm ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ € ์‚ฌ์šฉ

0-1. typeScript ์ „์—ญ์ ์œผ๋กœ ์„ค์น˜

npm install -g typescript

๐ŸŸฆ 1. ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ•  ํด๋” ์ƒ์„ฑ

๐ŸŸฆ 2. ํ”„๋กœ์ ํŠธ ์„ธํŒ…

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋กœ ์ด๋™ํ•˜์—ฌ ์ง„ํ–‰

npm init -y
tsc --init --rootDir ./src --outDir ./dist --esModuleInterop --module commonjs --strict true --allowJS true --checkJS true

์ฐธ๊ณ  :
--rootDir ./src : ํ”„๋กœ๊ทธ๋žจ์˜ ์†Œ์Šค ํŒŒ์ผ์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ๋กœ๋Š” src ๋””๋ ‰ํ† ๋ฆฌ
--outDir ./dist : ์ปดํŒŒ์ผ์ด ๋œ ํŒŒ์ผ๋“ค์ด ๋“ค์–ด๊ฐ€๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋Š” dist ๋””๋ ‰ํ† ๋ฆฌ
--esModuleInterop : CommonJS ๋ฐฉ์‹์˜ ๋ชจ๋“ˆ์„ ES๋ชจ๋“ˆ ๋ฐฉ์‹์˜ import ๊ตฌ๋ฌธ์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ.

๐ŸŸฆ 3. package.json ์— script ์ถ”๊ฐ€

"scripts": {
    "start": "tsc && node ./dist/index.js",
    "build": "tsc --build",
    "clean": "tsc --build --clean"
},

๐ŸŸฆ 4. ๋””๋ ‰ํ† ๋ฆฌ์— ์‚ฌ์šฉํ•  src ํด๋” ์ƒ์„ฑ

๐ŸŸฆ 5. ๋กœ์ง์ถ”๊ฐ€ (์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์›ํ•˜๋Š” ๋กœ์ง์„ ์งœ๋ฉด ๋จ)

interface Student {
  name: string;
  age: number;
  scores: {
    korean: number;
		math: number;
		society: number;
    science: number;
    english: number;
  };
}

function createStudent(name: string, age: number, korean: number, math: number, society: number, science: number, english: number): Student {
  return {
    name,
    age,
    scores: {
			korean,
      math,
			society,
      science,
      english,
    },
  };
}

function calculateAverage(student: Student): number {
  const sum = student.scores.korean + student.scores.math + student.scores.society + student.scores.science + student.scores.english;
  const average = sum / 5;
  return average;
}

function assignGrade(average: number): string {
  if (average >= 90) {
    return 'A';
  } else if (average >= 80) {
    return 'B';
  } else if (average >= 70) {
    return 'C';
  } else if (average >= 60) {
    return 'D';
  } else {
    return 'F';
  }
}

function printResult(student: Student): void {
  const average = calculateAverage(student);
  const grade = assignGrade(average);
  console.log(`${student.name} (${student.age}์„ธ) - ํ‰๊ท : ${average.toFixed(2)}, ํ•™์ : ${grade}`);
}

function main(): void {
	const spartan = createStudent('Spartan', 30, 95, 89, 76, 90, 97);
	printResult(spartan);
}

main(); 
๋กœ์ง ์ถ”๊ฐ€


๐ŸŸฆ 6. tsํŒŒ์ผ ๋‹ค ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ๋นŒ๋“œํ•˜๊ธฐ

์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ์žˆ์œผ๋ฉด ์ด๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ.
ts -> js ๋ฐ”๊ฟ”์ฃผ๋Š”๋“ฏ

npm run build 
  • ๋นŒ๋“œํ•˜๋Š” ๊ณผ์ •์—์„œ dist ํด๋”๊ฐ€ ์ƒ์„ฑ๋จ

    dist ํด๋”๋Š” ๋ญ˜๊นŒ?

    • distribution์˜ ์•ฝ์ž
    • src์˜ typescript ์ฝ”๋“œ๊ฐ€ nodejs์—์„œ ์ž‘๋™ํ•˜๊ธฐ ์œ„ํ•ด javascript ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ๋œ ๊ฒฐ๊ณผ๋ฌผ.

๐ŸŸฆ 7. js ํŒŒ์ผ ์‹คํ–‰

npm run start

์ฐธ๊ณ :
https://myung-ho.tistory.com/94
https://inpa.tistory.com/entry/TS-%F0%9F%93%98-%ED%83%80%EC%9E%85%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-tsconfigjson-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0-%EC%B4%9D%EC%A0%95%EB%A6%AC

profile
์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๋ฟ ๋‚ด๊ฐ€ ๋ชปํ•  ๊ฑด ์—†๋‹ค.

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