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

์นดํƒˆ๋กœ๊ทธ
  1. 1. 10์žฅ ํƒ€์ž… ์„ ์–ธ๊ณผ ๋ณ€๊ฒฝ, ํƒ€์ž… ํ˜ธํ™˜
    1. 1.1. ๐Ÿ“342p. ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ ํƒ€์ž… ๋ณ€ํ™˜์˜ ์ฐจ์ด
    2. 1.2. ๐Ÿ“343p. ํƒ€์ž… ์บ์ŠคํŒ…(type casting)๊ณผ ํƒ€์ž… ์–ด์„ค์…˜(type assertion) ์ฐจ์ด
    3. 1.3. ๐Ÿ“344p. ํƒ€์ž… ์–ด์…œ์„  ์„ ์–ธ ๋ฐฉ๋ฒ•
    4. 1.4. ๐Ÿ“346p. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋งˆ๋‹ค์˜ ํƒ€์ž… ๊ฒ€์‚ฌ ๋ฐฉ์‹๊ณผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ํƒ€์ดํ•‘ ๋ฐฉ์‹
      1. 1.4.1. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ 4๊ฐ€์ง€ ํƒ€์ž… ์ง€์ • ๋ฐฉ์‹
      2. 1.4.2. ๋• ํƒ€์ดํ•‘
      3. 1.4.3. ๊ตฌ์กฐ ํƒ€์ดํ•‘
        1. 1.4.3.1. 1. ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ
        2. 1.4.3.2. 2. ์ƒ์† ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•œ ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ
        3. 1.4.3.3. 3. ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ„์˜ ๊ตฌ์กฐ ํƒ€์ดํ•‘
      4. 1.4.4. ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘
        1. 1.4.4.1. ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘์˜ ์กฐ๊ฑด
        2. 1.4.4.2. 1. ํƒ€์ž…์ด ์—†์ง€๋งŒ ๊ตฌ์กฐ๊ฐ€ ์ผ๋ถ€ ๊ฐ™์€ ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘
        3. 1.4.4.3. 2. ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜ ํƒ€์ž…๊ฐ„์˜ ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘
        4. 1.4.4.4. 3. ๊ตฌ์กฐ๊ฐ€ ์ผ๋ถ€ ๊ฐ™์€ ๊ฐ์ฒด์™€ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ„์˜ ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘
      5. 1.4.5. ๋ช…๋ชฉ ํƒ€์ดํ•‘

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

10์žฅ ํƒ€์ž… ์„ ์–ธ๊ณผ ๋ณ€๊ฒฝ, ํƒ€์ž… ํ˜ธํ™˜

๐Ÿ“342p. ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ ํƒ€์ž… ๋ณ€ํ™˜์˜ ์ฐจ์ด

๋ช…์‹œ์ ์ธ ๊ฒƒ์˜ ์ฐจ์ด

ํƒ€์ž… ์บ์ŠคํŒ…(type casting): ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•œ ์บ์ŠคํŒ… ์ฝ”๋“œ์— ์˜ํ•œ ํƒ€์ž… ๋ณ€๊ฒฝ

ํƒ€์ž… ๋ณ€ํ™˜(type conversion): JS ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์˜ํ•œ ํƒ€์ž… ๋ณ€๊ฒฝ

1
2
3
4
5
6
7
8
9
10
11
12
// Type casting
let a: number = 3;
let b: string;
b = String(a); // ๋ช…์‹œ์ ์ธ ํƒ€์ž… ์บ์ŠคํŒ…

// Type conversion
let a = 3;
let b = ``;

console.log(typeof b); // string
b = a; // ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜
console.log(typeof b); // number

๐Ÿ“343p. ํƒ€์ž… ์บ์ŠคํŒ…(type casting)๊ณผ ํƒ€์ž… ์–ด์„ค์…˜(type assertion) ์ฐจ์ด

์ปดํŒŒ์ผ ์ดํ›„์˜ ์œ ์ง€ ์—ฌ๋ถ€

ํƒ€์ž… ์บ์ŠคํŒ…์˜ ๊ฒฝ์šฐ: ์ปดํŒŒ์ผ ์ดํ›„, ์ฝ”๋“œ ์œ ์ง€

ํƒ€์ž… ์–ด์„ค์…˜์˜ ๊ฒฝ์šฐ: ์ปดํŒŒ์ผ ๊ณผ์ •๊นŒ์ง€๋งŒ ์œ ํšจ, ์ปดํŒŒ์ผ ์ดํ›„ ์ œ๊ฑฐ

๐Ÿ“344p. ํƒ€์ž… ์–ด์…œ์„  ์„ ์–ธ ๋ฐฉ๋ฒ•

1
2
3
4
5
// ๋ฐฉ๋ฒ• 1
let num4: number = <number>myNum;

// ๋ฐฉ๋ฒ• 2
let num5: number = myNum as number;

๋ฐฉ๋ฒ•1 (๊บพ์‡  ๋ฐฉ์‹)์˜ ๊ฒฝ์šฐ๋Š” JSX ๋ฌธ๋ฒ•๊ณผ ์œ ์‚ฌํ•ด ์ถฉ๋Œ ์œ„ํ—˜์ด ์กด์žฌํ•˜์—ฌ, as๋ฅผ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ

๐Ÿ“346p. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋งˆ๋‹ค์˜ ํƒ€์ž… ๊ฒ€์‚ฌ ๋ฐฉ์‹๊ณผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ํƒ€์ดํ•‘ ๋ฐฉ์‹

  1. ๋™์  ํƒ€์ž… ๊ฒ€์‚ฌ(dynamic type checking): Javascript์˜ duck typing - ๋Ÿฐํƒ€์ž„ ์‹œ์ 
  2. ์ •์  ํƒ€์ž… ๊ฒ€์‚ฌ(static type chcking): C++, Java - ์ปดํŒŒ์ผ ์‹œ์ 

ํƒ€์ž… ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฒฝ์šฐ๋Š” ๋‘๊ฐœ ๋ชจ๋‘ ์ง€์›

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ 4๊ฐ€์ง€ ํƒ€์ž… ์ง€์ • ๋ฐฉ์‹

์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์˜ ์ด๋ฆ„์€ ๋ชฐ๋ผ๋„ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  1. ๋• ํƒ€์ดํ•‘(Duck typing)
  2. ๊ตฌ์กฐ ํƒ€์ดํ•‘(structural typing)
  3. ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘(structural subtyping)
  4. ๋ช…๋ชฉ ํƒ€์ดํ•‘(nominal typing)

๋• ํƒ€์ดํ•‘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
interface DuckGoose {
speak();
swim();
}

class Duck {
speak() {
console.log(`๊ฝฅ`);
}
swim() {
console.log(`์ˆ˜์˜ ์ค‘...`);
}
}

class Goose {
speak() {
console.log(`๊ตฌ์šฐ`);
}
swim() {
console.log(`๊ฑฐ์œ„ ์ˆ˜์˜ ์ค‘...`);
}
}

function swim(obj: DuckGoose) {
obj.speak();
obj.swim();
}

let duck = new Duck();
let goose = new Goose();

swim(duck);
swim(goose);

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋Ÿฐํƒ€์ž„์‹œ ๋™์ ์œผ๋กœ ํƒ€์ดํ•‘์ด ์ด๋ค„์ง€๋Š” ํƒ€์ž… ์ง€์ • ๋ฐฉ์‹

๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ, ์„ ์–ธ๋˜์ง€ ์•Š์€ ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ

๊ตฌ์กฐ ํƒ€์ดํ•‘

1. ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Animal {
name: number;
constructor(name: string, weight: number) {}
}

class Bird {
name: number;
constructor(speed: number) {}
}

let animal: Animal = new Animal(`happy`, 100);
let bird: Bird = new Bird(10);

// ํƒ€์ž… ํ˜ธํ™˜ ๊ฐ€๋Šฅ
animal = bird;
bird = animal;

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ํƒ€์ž… ํ˜ธํ™˜์ด ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ๊ฒ€์‚ฌ

ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ ์„œ๋กœ ํƒ€์ž… ํ˜ธํ™˜์ด ๊ฐ€๋Šฅ

์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ƒ๊ด€์—†์Œ, ์ ‘๊ทผ ์ œํ•œ์ž๊ฐ€ ์„ค์ •๋˜์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ
(์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’ private)

2. ์ƒ์† ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•œ ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Person {
public name: string;
}

class Member extends Person {
public grade: number;
}

class Admin extends Member {}

class MemberCard {
public name: string;
public grade: number;
}

let admin: Admin = new Admin();
admin = new MemberCard(); // ํƒ€์ž… ํ˜ธํ™˜์ด ๊ฐ€๋Šฅ

admin๊ณผ MemberCard๋Š” ์•„๋ฌด๋Ÿฐ ๊ด€๊ณ„๊ฐ€ ์—†์ง€๋งŒ, ์™„์ „ํžˆ ๋™์ผํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๊ฐ™์€ ๊ตฌ์กฐ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์†Œ์œ ํ•˜๊ณ  ์žˆ์œผ๋‚˜ ํƒ€์ž… ํ˜ธํ™˜์ด ๊ฐ€๋Šฅ

3. ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์€ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ„์˜ ๊ตฌ์กฐ ํƒ€์ดํ•‘

ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌ์กฐ ๊ฐ™์œผ๋ฉด ํƒ€์ž… ํ˜ธํ™˜์ด ๊ฐ€๋Šฅ

1
2
3
4
5
6
7
8
9
10
interface Person {
name: string;
}

class Employee {
name: string;
}

let person: Person;
person = new Employee(); // ํƒ€์ž… ํ˜ธํ™˜์ด ๊ฐ€๋Šฅ

๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘

ํƒ€์ž… ๊ตฌ์กฐ๊ฐ€ ๊ฐ™์•„์•ผ์ง€๋งŒ ํƒ€์ž… ํ˜ธํ™˜์ด ์ด๋ค„์ง€์ง€๋งŒ, ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘์€ ๊ตฌ์กฐ๊ฐ€ ๋ถ€๋ถ„์ ์œผ๋กœ ๊ฐ™๋”๋ผ๋„ ํƒ€์ž… ํ˜ธํ™˜์„ ์ง€์›

๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘์˜ ์กฐ๊ฑด

ํ•˜์œ„ ํƒ€์ž…์ด ์ƒ์œ„ ํƒ€์ž…์œผ๋กœ๋งŒ ํ˜ธํ™˜

1
2
3
4
5
6
7
8
9
10
11
12
// ์ƒ์œ„ ํƒ€์ž…
interface TypeA {
a: string;
b: string;
}

// ํ•˜์œ„ ํƒ€์ž…
interface TypeB {
a: string;
b: string;
c: string;
}

1. ํƒ€์ž…์ด ์—†์ง€๋งŒ ๊ตฌ์กฐ๊ฐ€ ์ผ๋ถ€ ๊ฐ™์€ ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘

1
2
3
4
5
6
7
8
9
10
11
12
let infoUpper = {
name: "taeuk",
country: "korea"
};

let infoSub = {
name: "taeuk",
country: "korea",
status: "happy"
}

infoUpper = infoSub;

2. ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜ ํƒ€์ž…๊ฐ„์˜ ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘

1
2
3
4
5
6
let funcUpper = (a: string) => a;
let funcSub = (a: string, b: string): string => a + b;

funcSub = funcUpper; // ํ•˜์œ„ ํƒ€์ž… = ์ƒ์œ„ ํƒ€์ž…

console.log(funcSub(`hello`, `world`));

3. ๊ตฌ์กฐ๊ฐ€ ์ผ๋ถ€ ๊ฐ™์€ ๊ฐ์ฒด์™€ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ„์˜ ๊ตฌ์กฐ ์„œ๋ธŒํƒ€์ดํ•‘

1
2
3
4
5
6
7
8
9
10
11
interface GroupUpper {
name: string;
}

let groupSub = {
name: `Typescrript Group`,
id: 1,
}

let groupUpper: GroupUpper;
groupUpper = groupSub;

๋ช…๋ชฉ ํƒ€์ดํ•‘

๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •๋œ ํƒ€์ž… ๊ฐ„์—๋งŒ ํƒ€์ž…์ด ํ˜ธํ™˜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum EastAsia1 {
korea = 88,
china = 86,
japan = 81,
}

enum EastAsia2 {
korea = 88,
china = 86,
japan = 81,
}

let east1: EastAsia1 = EastAsia1.china;
let ease2: EastAsia2 = EastAsia2.korea;

// ๊ตฌ์กฐ์ ์œผ๋กœ๋Š” ๊ฐ™์ง€๋งŒ, ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š์Œ
// let east1: EastAsia1 = EastAsia2.china;
// let east2: EastAsia2 = EastAsia1.korea;

east1 = 1000;
east2 = 2000;