Quick Start Typescript ~ 5์žฅ ์ •๋ฆฌ

์นดํƒˆ๋กœ๊ทธ
  1. 1. ๐Ÿ“p134. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž
  2. 2. ๐Ÿ“p136. ๋น„๊ต ์—ฐ์‚ฐ์ž
  3. 3. ๐Ÿ“p137. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
  4. 4. ๐Ÿ“p139 ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง
    1. 4.1. ์ข…๋ฅ˜
    2. 4.2. 1. ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง
      1. 4.2.1. rename
      2. 4.2.2. ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
      3. 4.2.3. type ํ‚ค์›Œ๋“œ ํ™œ์šฉ
    3. 4.3. 2. ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง
  5. 5. ๐Ÿ“p146. ์ „๊ฐœ ์—ฐ์‚ฐ์ž
    1. 5.1. ๋ฐฐ์—ด
    2. 5.2. ๊ฐ์ฒด

๐Ÿ“– Quick Start Typescript ์„ ์ฝ๊ณ , ๊ฐ„๋‹จํžˆ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„์ด๋‚˜ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

Typescript ํ•™์Šตํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์ดํ•ด๋„๊ฐ€ ๋†’์ง€ ๋ชปํ•ด, ์ž‘์„ฑ์‹œ ์ฐธ๊ณ ํ•œ ๋งํฌ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“p134. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž

ES7์˜ ์ง€์ˆ˜ ์—ฐ์‚ฐ์ž(Math.pow)๋ฅผ ์ง€์›, **๋ฅผ ์ด์šฉ

์‚ฌ์šฉ ์˜ˆ์‹œ

1
console.log(10 ** 3);	// 1000

๐Ÿ“p136. ๋น„๊ต ์—ฐ์‚ฐ์ž

๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋‹ค๋ฅด๊ฒŒ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” === ๋“ฑํ˜ธ 3๊ฐœ๊ฐ€ ์กด์žฌ, ==์™€ ๋‹ค๋ฅธ ์ ์€ ==์€ ๊ฐ’๋งŒ ๋น„๊ตํ•œ๋‹ค๋ฉด ===์€ ๊ฐ’๊ณผ ํƒ€์ž…์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ๋‹ค๋ฆ„

1
2
'1' == 1;	// true
'1' === 1; // false

์œ„ ์‚ฌํ•ญ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€์ง€๋งŒ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ™์€ ํƒ€์ž…๊ณผ์˜ ๋น„๊ต๋งŒ ์ง€์›ํ•œ๋‹ค.

๐Ÿ“p137. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” ์ฃผ๋กœ ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ํ•จ๊ป˜ ํ•จ์ˆ˜ ์ดˆ๊ธฐ๊ฐ’ ์„ ์–ธ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

์‚ฌ์šฉ ์˜ˆ์‹œ

1
2
3
4
5
6
7
8
// ์ด๋Ÿฐ์‹์œผ๋กœ ๊ธฐ๋ณธ๊ฐ’์ด ์—†์œผ๋ฉด, default๋ฅผ ๋งŒ๋“ ๋‹ค. ๋ผ๋Š” ๊ฒƒ์œผ๋กœ ์„ ์–ธ ๊ฐ€๋Šฅ
const value: string = value || 'default';

// ๋ถ€์ • ์—ฐ์‚ฐ์ž๋Š” depth๋ฅผ ๊นŠ๊ฒŒํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•œ, if๋ฌธ์— ๋งŽ์ด ์‚ฌ์šฉ๋จ
if (!isLogin) {
console.error(`no login!`);
location.href = `/login`; // login ๋ผ์šฐํŒ…์œผ๋กœ ์ด๋™
}

๐Ÿ“p139 ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง

์ข…๋ฅ˜

  1. ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง
  2. ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง

1. ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง

1
2
3
4
let { id, country = 88 } = { id: `happy` };

console.log(id); // happy
console.log(country); // 88

rename

1
2
3
4
5
let { id: newName1, country: newName2 } = { id: `happy`, country: 88 };
console.log(newName1); // happy
console.log(newName2); // 88

console.log(id, country); // error Not defined

ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •

1
2
3
4
5
test({ name: `happy` });	// happy, none ์ถœ๋ ฅ

function test({ name, country = `none` }) {
console.log(name, country);
}

type ํ‚ค์›Œ๋“œ ํ™œ์šฉ

1
2
3
4
5
6
7
8
type C = { a: string, b?: number };

fruit({ a: `apple`, b: 10 }); // apple 10
fruit({ a: `apple`}); // apple undefined

function fruit({ a, b }: C) {
console.log(a, b);
}

2. ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
let numbers = [`one`, `two`, `three`, `four`, `five`];

// ์ด์ „ ๋ฐฉ์‹
let num1 = numbers[0]; // one
let num2 = numbers[1]; // two


// ์ดํ›„ ๋ฐฉ์‹
let [num1, num2] = numbers; // one, two

// ๊ฒฐ๊ณผ ๋™์ผ
console.log(num1, num2); // one, two

// ์ค‘๊ฐ„ ๋นผ์˜ค๊ธฐ
let [, , num3, num4, ] = numbers;
console.log(num3, num4); // three, four

// ๊ต์ฒด
[num4, num3] = [num3, num4];
console.log(num3, num4); // four, three

// ์ดˆ๊ธฐ๊ฐ’ ์ง€์ •
let [color1, color2 = `blue`] = [`black`];
console.log(color1, color2); // black blue

๐Ÿ“p146. ์ „๊ฐœ ์—ฐ์‚ฐ์ž

๋ฐฐ์—ด

1
2
3
let [first, ...second] = [1, 2, 3];

console.log(first, second); // 1 2, 3

๊ฐ์ฒด

1
2
3
let numGroup = { n1: 1, n2: 2, n3: 3};
let { n2, ...n13 } = numGroup;
console.log(n2, n13); // 2 { n1: 1, n3: 3};