Jieunny์˜ ๋ธ”๋กœ๊ทธ

[TS] 0. TypeScript๋ž€? ๋ณธ๋ฌธ

Study/TypeScript

[TS] 0. TypeScript๋ž€?

Jieunny 2023. 1. 26. 09:55

๐Ÿ“ฃ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ž€?

โœ”๏ธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ํƒ€์ž…์„ ๋ถ€์—ฌํ•œ ์–ธ์–ด

โœ”๏ธ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰ํ•˜๋ ค๋ฉด ํŒŒ์ผ์„ ํ•œ๋ฒˆ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค(์ปดํŒŒ์ผ)

 

๐Ÿ“ฃ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ์ปดํŒŒ์ผ๋Ÿฌ tsc 

โœ”๏ธ ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ

npm install -g typescript
tsc hello.ts

โžฐ hello.js ํŒŒ์ผ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ฃ  ์™œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์จ์•ผ ํ•˜๋Š”๊ฐ€?

โœ”๏ธ ์—๋Ÿฌ์˜ ์‚ฌ์ „ ๋ฐฉ์ง€

// JS ์ฝ”๋“œ
function sum(a, b) {
	return a + b;
}

sum('10', '20); // 1020
// TS ์ฝ”๋“œ
function sum(a: number, b:number) {
	return a + b;
}

sum('10', '20'); // Error: '10'์€ number์— ํ• ๋‹น๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

โžฐ ์˜๋„ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ์˜ ๋™์ž‘์„ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ”๏ธ ์ฝ”๋“œ ์ž๋™ ์™„์„ฑ๊ณผ ๊ฐ€์ด๋“œ

function sum(a: number, b: number): number {
  return a + b;
}
var total = sum(10, 20);
total.toLocaleString();

โžฐ total์— ๋Œ€ํ•œ ํƒ€์ž…์ด ๋ฏธ๋ฆฌ ์ง€์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— VSCode์—์„œ ํ•ด๋‹น ํƒ€์ž…์— ๋Œ€ํ•œ API๋ฅผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋กœ ๋„์›Œ์ค€๋‹ค.

 

๐Ÿ“ฃ  ๋ช…์‹œ์  ํƒ€์ž…

function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
 
greet("Maddison", Date());
// Argument of type 'string' is not assignable to parameter of type 'Date'.

greet("Maddison", new Date());

โžฐ Date()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด string์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ์ธ์ž๋กœ ์ฃผ์—ˆ๋˜ Date ํƒ€์ž…๊ณผ ๋งž์ง€ ์•Š์•„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

 

let msg = 'hello there!";
// ๋งˆ์šฐ์Šค ํ˜ธ๋ฒ„ํ•˜๋ฉด ํ™”๋ฉด์— 'let msg: string' ์ด๋ผ๊ณ  ๋œฌ๋‹ค.

โžฐ msg๊ฐ€ string ํƒ€์ž…์„ ๊ฐ–๋Š”๋‹ค๋Š” ๊ฑด TS์—๊ฒŒ ์•Œ๋ ค์ฃผ์ง€ ์•Š์•„๋„ ์•Œ์•„์„œ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

โžฐ ์–ด๋–ป๊ฒŒ๋“  ํƒ€์ž… ์‹œ์Šคํ…œ์ด ์•Œ์•„์„œ ํƒ€์ž…์„ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ตณ์ด ํƒ€์ž… ํ‘œ๊ธฐ๋ฅผ ์ ์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

๐Ÿ“ฃ  ์ง€์›Œ์ง„ ํƒ€์ž… 

function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
"use strict";
function greet(person, date) {
    console.log("Hello ".concat(person, ", today is ").concat(date.toDateString(), "!"));
}
greet("Maddison", new Date());

โžฐ ์œ„์˜ .tsํŒŒ์ผ์„ ๋ณ€ํ™˜ํ•˜๋ฉด ๋ฐ‘์˜ .jsํŒŒ์ผ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

โžฐ ๋Œ€๋ถ€๋ถ„์˜ TS ์ „์šฉ ์ฝ”๋“œ๋Š” ์ œ๊ฑฐ๋˜๊ณ , ํƒ€์ž… ํ‘œ๊ธฐ ๋˜ํ•œ ์™„์ „ํžˆ ์ง€์›Œ์ง„๋‹ค.

 

๐Ÿ“ฃ  ๋‹ค์šด๋ ˆ๋ฒจ๋ง : ์ƒ์œ„ ๋ฒ„์ „์˜ ECMAScript๋ฅผ ํ•˜์œ„ ๋ฒ„์ „์˜ ๊ฒƒ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ

`Hello ${person}, today is ${date.toDateString()}!`;
"Hello " + person + ", today is " + date.toDateString() + "!";

โžฐ ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด์€ ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž(+)๋กœ ์ด๋ฃจ์–ด์ง„ ์ผ๋ฐ˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

โžฐ ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด์€ ECMAScript 2015์—์„œ ๋“ฑ์žฅํ•œ ๊ธฐ๋Šฅ์ด๊ณ , TS๋Š” ์ƒˆ ๋ฒ„์ „์˜ ์ฝ”๋“œ๋ฅผ ECMAScript3 ๋˜๋Š” ECMAScript5 ๊ฐ™์€ ์˜ˆ์ €๋„ˆ ๋ฒ„์ „์˜ ๊ฒƒ๋“ค๋กœ ๋‹ค์‹œ ์ž‘์„ฑํ•ด ์ค€๋‹ค.

 

๐Ÿ“ฃ  ์—„๊ฒฉ๋„

โœ”๏ธ ์ผœ๊ณ  ๋Œ ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€์˜ ํƒ€์ž… ๊ฒ€์‚ฌ ์—„๊ฒฉ๋„ ํ”Œ๋ž˜๊ทธ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

โžฐ --strict ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ tsconfig.json์— "strict":true ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ชจ๋“  ํ”Œ๋ž˜๊ทธ๋ฅผ ๋™์‹œ์— ํ™œ์„ฑํ™” ํ•œ๋‹ค.

โžฐ noImplicitAny : ํƒ€์ž…์ด any๋กœ ์•”๋ฌต์ ์œผ๋กœ ์ถ”๋ก ๋˜๋Š” ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

โžฐ strictNullChecks : null๊ณผ undefined๋ฅผ ๋ณด๋‹ค ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

 

๐Ÿ“š ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ•ธ๋“œ๋ถ

https://www.typescriptlang.org/ko/docs/handbook

https://joshua1988.github.io/ts

'Study > TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TS] ํ•จ์ˆ˜์™€ ๋ฉ”์„œ๋“œ  (0) 2023.02.01
[TS] ๊ฐ์ฒด์™€ ํƒ€์ž…  (2) 2023.02.01
[TS] ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ  (0) 2023.01.30
[TS] 2. Usage  (0) 2023.01.27
[TS] 1. Fundamentals  (0) 2023.01.26