[TS] TypeScript?

๋‹ˆ๋‚˜๋…ธ๊ฐœ๋ฐœ์ƒํ™œยท2021๋…„ 9์›” 27์ผ
0

๐Ÿ’กah-ha

๋ชฉ๋ก ๋ณด๊ธฐ
34/51
post-thumbnail

TypeScript

  • Typed Superset of JavaScript
  • compiles to plain JavaScript
  • Programming Language
  • Compiled Language(์ „ํ†ต๊ณผ๋Š” ๋‹ค๋ฅธ ์ ์ด ๋งŽ์•„์„œ Transpile์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉ)
    -> Editor์—์„œ TS๋กœ ์ž‘์—…์„ ํ•˜๋ฉด TypeScript Compiler๋ฅผ ํ†ตํ•ด Plain JavaScript๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‹คํ–‰

โ˜๐Ÿป watch ๋ชจ๋“œ

tsc --w
  • watch ๋ชจ๋“œ์ผ ๊ฒฝ์šฐ ๋ณ„๋„์˜ ์ปดํŒŒ์ผ์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์•„๋„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•œ๋‹ค.

Type Annotation

  • ํŠน์ •ํ•œ ๊ฐ์ฒด ๋“ฑ์˜ ํƒ€์ž…์„ ์ง€์ •ํ•˜๋Š” ์ผ
let b: number;

b = 'ariel';
b = 10;

function hello(c: number) {

}
hello(39)
// ๋งค๊ฐœ๋ณ€์ˆ˜ c์˜ ํƒ€์ž…์„ number๋กœ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์ธ hello๋Š” ์˜ค๋ฅ˜
hello('hello')

TS vs JS

  • TS : Static Types
    -> ๊ฐœ๋ฐœ ์ค‘๊ฐ„์— ํƒ€์ž… ์ฒดํฌ
function add(n1: number, n2: number) {
  return n1 + n2;
}

const result = add(39, 28);
  • JS : Dynamic Types
    -> ๋Ÿฐํƒ€์ž„์— ๋Œ์ž…ํ•ด์•ผ ์˜ค๋ฅ˜ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•จ
function add(n1, n2) {
  if (typeof n1 !== 'number' || typeof n2 !== 'number') {
    throw new Error('Incorrect input!');
  }
  return n1 + n2;
}

const result = add(39, 28);
profile
๊นƒํ—™์œผ๋กœ ์ด์‚ฌ์ค‘..

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