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

์นดํƒˆ๋กœ๊ทธ
  1. 1. 9์žฅ ๊ณ ๊ธ‰ ํƒ€์ž…
    1. 1.1. ๐Ÿ“315p. ์œ ๋‹ˆ์–ธ ํƒ€์ž…
    2. 1.2. ๐Ÿ“317p. ํƒ€์ž… ๊ฐ€๋“œ
      1. 1.2.1. typeof
      2. 1.2.2. instanceof
    3. 1.3. ๐Ÿ“320p. ๊ณ ๊ธ‰ ํƒ€์ž…๋“ค
      1. 1.3.1. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…
      2. 1.3.2. ๋ฃฉ์—… ํƒ€์ž… (= ์ธ๋ฑ์Šค ํƒ€์ž…)
      3. 1.3.3. non-nullable ํƒ€์ž…
      4. 1.3.4. never ํƒ€์ž…
      5. 1.3.5. this ํƒ€์ž…
        1. 1.3.5.1. ์ธํ„ฐํŽ˜์ด์Šค์— this ์‚ฌ์šฉ ์˜ˆ์‹œ
        2. 1.3.5.2. ํ”Œ๋ฃจ์–ธํŠธ ์ธํ„ฐํŽ˜์ด์Šค ํŒจํ„ด (ํ”Œ๋ฃจ์–ธํŠธ ํŒจํ„ด)

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

9์žฅ ๊ณ ๊ธ‰ ํƒ€์ž…

๐Ÿ“315p. ์œ ๋‹ˆ์–ธ ํƒ€์ž…

1
const x: string | number;

๐Ÿ“317p. ํƒ€์ž… ๊ฐ€๋“œ

typeof

1
2
3
4
5
6
7
8
9
function myIndexOf(x: number | string, y: string) {
if (typeof x === `string`) {
return x.indexOf(y);
}

return -1;
}

console.log(myIndexOf(`hello`, `e`))

instanceof

1
2
3
4
5
6
7
8
function diff(x: Cat | Dog) {
if (x instanceof Dog) {
...
return;
}

return;
}

๐Ÿ“320p. ๊ณ ๊ธ‰ ํƒ€์ž…๋“ค

๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…

1
2
3
4
5
6
7
let event; 'keyup' = 'keyup';	// O
let event: 'keyup' = 'keyup2'; // error

// or

type EventType = 'keyup' | 'mouseover';
const myEvent: EventType = 'keyup';

๋ฃฉ์—… ํƒ€์ž… (= ์ธ๋ฑ์Šค ํƒ€์ž…)

keyof ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํƒ€์ž… T์˜ ํ•˜์œ„ ํƒ€์ž…์„ ์ƒ์„ฑ, ํƒ€์ž… T๋Š” ์—ฌ๋Ÿฌ ํƒ€์ž…์œผ๋กœ ์ด๋ค„์ง„ ์œ ๋‹ˆ์–ธ์ด๋‚˜ ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…์„ ์˜๋ฏธ

ํ™•์žฅ์„ฑ์„ ๊ณ ๋ คํ•ด interface๋ฅผ ๋„์ž…

1
2
3
4
5
interface Profile {
name: string;
gender: string;
age: number;
}

์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ keyof๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฃฉ์—… ํƒ€์ž…์œผ๋กœ ์„ ์–ธ

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
// # 1
// ์ด๋ ‡๊ฒŒ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” name , gener, age ์ค‘ ํ•˜๋‚˜๋ฅผ ํ• ๋‹น ๋ฐ›๊ธฐ ๊ฐ€๋Šฅ
type Profile1 = keyof Profile;

let pValue: Profile1 = 'name';

// # 2
// ๋ฐฐ์—ด ํƒ€์ž…์˜ ๋‚ด์žฅ ์†์„ฑ์ธ, length, push, pop, concat ๋“ฑ์„ ํ• ๋‹น๋ฐ›์•„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
type Profile2 = keyof Profile[];

let pValue2: Profile2 = 'length';
let pValue3: Profile2 = 'push';

// # 3
// ??? ์ดํ•ด ์•ˆ๋˜๋Š” ๋ถ€๋ถ„
// ์–ด๋Š ๋ฌธ์ž์—ด์ด๋“  ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ๊ฑด๊ฐ€?
type Profile3: keyof { [x: string]: Profile };

let pValue4: Profile3 = `hello`;

// # 4
// name์˜ string ํƒ€์ž…์„ ์ „๋‹ฌ, ํƒ€์ž…์ด string์ผ ๋•Œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋‚ด์žฅ ์†์„ฑ ์ด์šฉ ๊ฐ€๋Šฅ
type Profile4 = keyof Profile[`name`];

let pValue5: Profile4 = `length`;
let pValue6: Profile4 = `abcd`; // error

non-nullable ํƒ€์ž…

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2.0 ์ด์ „์—๋Š” null ์ด๋‚˜ undefined๋Š” ๋ชจ๋“  ํƒ€์ž…์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์—ˆ์Œ

๊ทธ๋Ÿฌ๋‚˜, tsconfig.json์— strictNullCheck์„ true๋กœ ๋ฐ”๊พธ๋ฉด, null๊ณผ undefined๊ฐ€ ์ž๋™์œผ๋กœ ๋ชจ๋“  ํƒ€์ž…์˜ ํ• ๋‹น๋˜์ง€ ์•Š๊ณ  ๋ณ„๋„๋กœ ํƒ€์ž…์œผ์˜ฌ ๊ด€๋ฆฌํ•ด์ค˜์•ผํ•จ.

never ํƒ€์ž…

never๋Š” ๋ชจ๋“  ํƒ€์ž…์˜ ํ•˜์œ„ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, any๋งŒ ํ• ๋‹น๋  ์ˆ˜ ์—†๋‹ค.

์‚ฌ์šฉ์šฉ๋„

  1. ํ•จ์ˆ˜์— ๋‹ฟ์„ ์ˆ˜ ์—†๋Š” ์ฝ”๋“œ ์˜์—ญ์ด ์žˆ์–ด ๋ฐ˜ํ™˜๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ
  2. ํ•จ์ˆ˜์— throw๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋˜์–ด, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ
1
2
3
4
5
6
7
const neverFunc = (): never => {
while (true) {

}
// console.log(); <- ๋‹ฟ์„ ์ˆ˜ ์—†์Œ
}
let resultNever: never = neverFunc();
1
2
3
function error(message: string): never {
throw new Error(message);
}

this ํƒ€์ž…

this ํƒ€์ž…์„ ๋‹คํ˜•์  this ํƒ€์ž…์ด๋ผ๊ณ ๋„ ํ•จ, ์„ ์–ธ ์œ„์น˜์— ๋”ฐ๋ผ ์ฐธ์กฐํ•˜๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ

์ธํ„ฐํŽ˜์ด์Šค์— this ์‚ฌ์šฉ ์˜ˆ์‹œ

1
2
3
4
interface ListItem {
getHead(): this;
getTail(): this;
}

ํ”Œ๋ฃจ์–ธํŠธ ์ธํ„ฐํŽ˜์ด์Šค ํŒจํ„ด (ํ”Œ๋ฃจ์–ธํŠธ ํŒจํ„ด)

๊ทธ๋ƒฅ ์ž๊ธฐ์ž์‹  ๋ฐ˜ํ™˜ํ•ด์„œ ์ฒด์ด๋‹ํ•˜๋Š” ํŒจํ„ด

๊ฐœ์ธ์ ์œผ๋กœ ์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ–ˆ์—ˆ์Œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const Mycalc = () => {
return {
val: 0,
plus(num) {
this.val += num;
return this;
},
minus(num) {
this.val -= num;
return this;
}
}
}

Mycalc().plus(3).minus(2).val // 1