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

์นดํƒˆ๋กœ๊ทธ
  1. 1. 11์žฅ ์ œ๋„ค๋ฆญ
    1. 1.1. ๐Ÿ“361p ์ œ๋„ค๋ฆญ ์†Œ๊ฐœ
      1. 1.1.1. ์žฅ์ 
      2. 1.1.2. ์‚ฌ์šฉ๋ฒ•
      3. 1.1.3. ์˜ˆ์ œ. ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ ๊ด€๋ จ ์ฝ”๋“œ ์ œ๊ฑฐ
    2. 1.2. ๐Ÿ“366p. ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ T๋ฅผ ํŠน์ • ํƒ€์ž…์œผ๋กœ ์ œํ•œํ•ด์•ผํ•  ๊ฒฝ์šฐ
    3. 1.3. ๐Ÿ“367p. ์˜ค๋ฒ„๋กœ๋“œ์‹œ์˜ ์ œ๋„ค๋ฆญ
    4. 1.4. ๐Ÿ“369p. ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ ์ด์ƒ ์„ ์–ธ๋ฒ•
    5. 1.5. ๐Ÿ“371p. Generic class
    6. 1.6. ๐Ÿ“377p. ๋ฃฉ์—… ํƒ€์ž…(lookup)
      1. 1.6.1. ํ™œ์šฉ ์˜ˆ์ œ
    7. 1.7. ๐Ÿ“379p. ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์†๊ณผ ๋™์‹œ์— ์ œ๋„ค๋ฆญ ํ™•์žฅ
    8. 1.8. ๐Ÿ“381p. ๋งต ๊ฐ์ฒด ์†Œ๊ฐœ
    9. 1.9. ๐Ÿ“384p. ์ œ๋„ค๋ฆญ ๊ธฐ๋ฐ˜์˜ ์ž๋ฃŒ๊ตฌ์กฐ ์ž‘์„ฑ

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

11์žฅ ์ œ๋„ค๋ฆญ

๐Ÿ“361p ์ œ๋„ค๋ฆญ ์†Œ๊ฐœ

์ œ๋„ค๋ฆญ(generics)์€ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜์— ํƒ€์ž…์ด ๊ณ ์ •๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์š”์†Œ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฒƒ

๊ธฐ์กด C#, JAVA์—์„œ ์ œ๊ณต๋˜์—ˆ๋˜ ๊ธฐ๋Šฅ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋Š” 0.9๋ถ€ํ„ฐ ์ง€์›

์žฅ์ 

  1. ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ์ง„ํ–‰ํ•ด ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ๋ณด์žฅ
  2. ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐ ๊ฐ€๋Šฅ
  3. ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์ž‘์„ฑ ๊ฐ€๋Šฅ

์‚ฌ์šฉ๋ฒ•

1
2
3
4
5
6
7
8
function arrayConcat<T>(array1: T[], array2: T[]): T[] {
return [...array1, ...array2];
}

const array1 = [1,2,3];
const array2 = [4,5,6];

const resultConcat = arrayConcat<number>(array1, array2);

<T> ๊ฐ€์ƒ์˜ ํƒ€์ž…(= ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜(type parameter) = ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋ณ€์ˆ˜(generic type variables))์œผ๋กœ ์ž„์˜์˜ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

์˜ˆ์ œ. ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ ๊ด€๋ จ ์ฝ”๋“œ ์ œ๊ฑฐ

์Œโ€ฆ ๊ฒฐ๊ตญ ๋ฌธ์ž์—ด๋งŒ ๋ฐ›์•„์„œ, ์บ์ŠคํŒ… ์ฝ”๋“œ๋ฅผ ์—†์• ๊ฑฐ๋‚˜, ์ˆซ์ž๋ฅผ ๋ฐ›์•„์„œ ์บ์ŠคํŒ… ์ฝ”๋“œ๋ฅผ ์ดํ›„ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฑฐ๋‚˜ ์กฐ์‚ผ๋ชจ์‚ฌ ์•„๋‹Œ๊ฐ€?

๊ทธ๋Ÿผ ๋ณธ์งˆ์ ์œผ๋กœ ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด ์•„๋‹Œ๊ฐ€?

์ด์ „

1
2
3
4
5
function concat(str1, str2) {
return String(str) + String(str2);
}

concat(`abc`, 123);

์ดํ›„

1
2
3
4
5
function concat<T>(str1: T, str2: T): T {
return str1 + str2;
}

concat<string>(`abc`, String(123)); // ์Œ ๊ฒฐ๊ตญ? ์บ์ŠคํŒ… ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•ด์ ธ๋ฒ„๋ฆผ

๐Ÿ“366p. ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ T๋ฅผ ํŠน์ • ํƒ€์ž…์œผ๋กœ ์ œํ•œํ•ด์•ผํ•  ๊ฒฝ์šฐ

1
2
3
4
5
// <T extends string>

function concat<T extends string>(str1: T, str2: T): string {
return str1 + str2;
}

๐Ÿ“367p. ์˜ค๋ฒ„๋กœ๋“œ์‹œ์˜ ์ œ๋„ค๋ฆญ

1
2
3
4
function concat<T>(str1: T, str2: T): T;
function concat(str1: any, str2: any) {
return str1 + str2;
}

๐Ÿ“369p. ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐœ ์ด์ƒ ์„ ์–ธ๋ฒ•

1
2
3
4
5
6
7
8
9
10
11
12
13
let mapArray = [];
function put<T, T2>(str1: T, str2: T2): T;
function put(idx: any, str: any) {
mapArray[idx] = str;
}

function get<T, T2>(idx: T): T2;
function get(idx: any) {
return mapArray[idx];
}

put<number, string>(1, `hello`);
console.log(get<number, string>(1));

๐Ÿ“371p. Generic class

1
2
3
4
5
6
7
class ํด๋ž˜์Šค๋ช…<T> {
๋ฉ”์†Œ๋“œ(param: Array<T>, param2: number): T {
return param[param2];
}
}

const ์ธ์Šคํ„ด์Šค๋ช… = new ํด๋ž˜์Šค๋ช…<number>(ํŒŒ๋ผ๋ฏธํ„ฐ);

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface IName {
name: string;
}

class Profile implements IName {
name: string = `taeuk`;
}

class Accessor<T extends IName> {
getKey(obj: T) {
return obj.name;
}
}

let acc = new Accessor();
console.log(acc.getKey(new Profile())); // LOG: taeuk

๐Ÿ“377p. ๋ฃฉ์—… ํƒ€์ž…(lookup)

keyof ํ‚ค์›Œ๋“œ๋กœ ํ‚ค๊ฐ’์„ ํƒ์ƒ‰ํ•˜์—ฌ, ์œ ๋‹ˆ์–ธ ํƒ€์ž…์ฒ˜๋Ÿผ ์ž‘๋™์‹œํ‚ด

์•„๋ž˜์˜ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด, ์ธํ„ฐํŽ˜์ด์Šค์˜ ํ‚ค๊ฐ’๋งŒ์„ ํ—ˆ์šฉํ•˜๋Š” ์œ ๋‹ˆ์–ธ ๋ฐฉ์‹์ฒ˜๋Ÿผ ์ž‘๋™๋จ์„ ์•Œ ์ˆ˜ ์žˆ์Œ

1
2
3
4
5
6
7
interface INumber {
one: number;
two: number;
three: number;
}

type NumberKeys = keyof INumber; // one, two, three, ์ธํ„ฐํŽ˜์ด์Šค ํ‚ค๊ฐ’๋งŒ์„ ํ—ˆ์šฉ

ํ™œ์šฉ ์˜ˆ์ œ

1
2
3
4
5
6
7
8
9
10
function getValue<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
let numbersKeys = {
one: 1,
two: 2,
three: 3,
};

console.log(getValue(numbersKeys, `one`)); // LOG: 1

๐Ÿ“379p. ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์†๊ณผ ๋™์‹œ์— ์ œ๋„ค๋ฆญ ํ™•์žฅ

1
2
3
4
5
6
7
8
9
10
11
12
13
interface IFilter<T> {
unique(array: Array<T>): Array<T>;
}

class Filter<T> implements IFilter<T> {
unique(array: Array<T>): Array<T> {
return array.filter((v, i, array) => array.indexOf(v) === i);
}
}

let myFilter = new Filter<string>();
let resultFilter = myFilter.unique([`a`, `b`, `c`, `a`, `b`]);
console.log(resultFilter); // LOG: [`a`, `b`, `c`]

๐Ÿ“381p. ๋งต ๊ฐ์ฒด ์†Œ๊ฐœ

์ƒ๋žต

๐Ÿ“384p. ์ œ๋„ค๋ฆญ ๊ธฐ๋ฐ˜์˜ ์ž๋ฃŒ๊ตฌ์กฐ ์ž‘์„ฑ

์Šคํƒ, ํ, ArrayList๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋‚ด์žฅ ๊ฐ์ฒด๋กœ ์ง€์›๋˜์ง€ ์•Š์•„์„œ ์ง์ ‘ ๊ตฌํ˜„์„ ํ•„์š”๋กœ ํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ

์ด ์ค‘ ์ฑ…์—์„œ๋Š” ArrayList๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ์„ค๋ช…

์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žต