JavaScript Class vs TypeScript Class
1. ์์ฑ ์ด๊ธฐํ ์ฝ๋๋ฅผ ์๋ตํ ์ ์๋ค.
- TS Class๋ ์์ฑ์์ ๋งค๊ฐ๋ณ์ ํ์ ์ ์ธ๊ณผ ๋์์ ์ ๊ทผ ์ ์ด์(+readonly)๋ง ์ฌ์ฉํ๋ฉด ์์ฑ ์ด๊ธฐํ๋ฅผ ํ ์ ์๋ค.
- ์ด๋๋ ์์ฑ์ด public์ด์ด๋ ๋ช ์ํด์ผ ํ๋ค.
// JS
class User {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
// TS
// ์ปดํ์ผํ๋ฉด ์ JS ์ฝ๋์ ๋์ผํ๋ค
class User {
constructor(private firstName: string, private lastName: string) {}
}
2. ์ ๊ทผ ์ ํ์(access modifier)๋ฅผ ์ง์ํ๋ค.
- ์ ๊ทผ ์ ํ์๋ TS ์คํ์ ๋๋ค. ๋ฐ๋ผ์ ์ปดํ์ผํ๋ฉด ์ฌ๋ผ์ง๋ค.
- JS์์๋ private๊ณผ ๊ฐ์ ์ฉ๋๋ก # prefix๋ฅผ ๋ถ์ธ๋ค.
- TS ์ ๊ทผ ์ ํ์์๋ public, protected, private์ด ์๋ค.
- public : ์ด๋์๋ ์์ ๋กญ๊ฒ ์ ๊ทผ ๊ฐ๋ฅ
- protected : ๋ด ํด๋์ค๋ฅผ ์์ํ ์์ ํด๋์ค ๋ด์์ ๊น์ง๋ง ์ ๊ทผ ๊ฐ๋ฅ
- private : ๋ด ํด๋์ค์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
- JS, TS ๋ชจ๋ ์ ๊ทผ ์ ํ์๋ฅผ ์๋ตํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก public์ด๋ค.
3. ์ถ์ ํด๋์ค(abstract class)๋ฅผ ์ง์ํ๋ค.
- ์ถ์ ํด๋์ค๋ TS ์คํ์ด๋ค. ์ปดํ์ผํ๋ฉด abstract ํค์๋๊ฐ ์ฌ๋ผ์ง ์ผ๋ฐ ํด๋์ค๊ฐ ๋๋ค.
- ์์๋ฐ๋ ํด๋์ค๊ฐ ์ด๋ป๊ฒ ๋์ํด์ผํ ์ง ์๋ ค์ฃผ๊ธฐ ์ํ ํด๋์ค๋ก, interface์ ์ ์ฌํ๋ค.
- interface๊ฐ ํด๋์ค๋ ๊ฐ์ฒด์ ๋ผ๋๋ฅผ ๋ง๋ ๋ค๋ฉด, ์ถ์ ํด๋์ค๋ ํด๋์ค ์ ์ฉ์ผ๋ก ํด๋์ค์ ๋ผ๋๋ฅผ ๋ง๋๋ ์ญํ ์ด๋ค.
- ํ์ง๋ง abstract class๋ก๋ ์ง์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค ์ ์๋ค. extends ํค์๋๋ก ์์ ํด๋์ค์ ์์์์ผ์ผ ํ๋ค.
- ์์ฑ์ด๋ ๋ฉ์๋ ์์ abstract ํค์๋๋ฅผ ๋ถ์ฌ์ ๋๋ฅผ ์์ํ๋ ๋ค๋ฅธ ํด๋์ค์์ ์ด ์์ฑ์ด๋ ๋ฉ์๋๋ฅผ ๊ตฌํํ๊ฒ ํ๋ค.
- ์ถ์ ๋ฉ์๋๋ ์ถ์ ํด๋์ค์ ์ ์ธ๋๋ ๋ฉ์๋๋ก, call signature๋ง ์์ฑํ๋ค.
- ์ถ์ ๋ฉ์๋๊ฐ ์๋ ๊ฒฝ์ฐ, ์ถ์ ํด๋์ค๋ฅผ ์์๋ฐ๋ ์์ ํด๋์ค๋ ์ถ์ ๋ฉ์๋๋ฅผ ๋ฐ๋์ ๊ตฌํํด์ผ ํ๋ค.
abstract class User {
constructor(protected firstName: string, protected lastName: string) {}
abstract getFullName(): void;
}
class Player extends User {
constructor(private firstName: string, private lastName: string) {}
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
}
const player = new Player("Timothee", "Chalamet");
player.getFullName();
Interface
- interface๋ ํด๋์ค๋ ๊ฐ์ฒด์ ๋ชจ์์ ๋ง๋ค์ด์ค๋ค.
- interface๋ TS ์คํ์ผ๋ก, ์ปดํ์ผํ๋ฉด ์ฌ๋ผ์ง๋ค.
- interface๋ ์ ์ธ์ ๋ณํฉํ ์ ์๋ค.
interface User {
firstName: string
}
interface User {
lastName: string
}
interface User {
health: number
}
const user: User {
firstName: "Timothee",
lastName: "Chalamet",
health: 10
}
- extends ํค์๋๋ก interface๋ฅผ ํ์ฅํ์ฌ ์๋ก์ด interface๋ฅผ ๋ง๋ค ์ ์๋ค.
interface Animal {
name: string;
}
interface Bear extends Animal {
honey: boolean;
}
const bear = getBear();
bear.name;
bear.honey;
Type
- type์ ๊ฐ์ฒด์ ๋ชจ์์ ๋ง๋ค๊ฑฐ๋, ํน์ ๊ฐ์ ๊ฐ์ง๋๋ก ์ ํํ ์๋ ์๋ค.
- type์ TS ์คํ์ผ๋ก, ์ปดํ์ผํ๋ฉด ์ฌ๋ผ์ง๋ค.
- type์ ํ์ ๋ณ์นญ(type alias)์ ๋ง๋ค ์ ์๋ค.
type Health = 1 | 5 | 10;
type Team = "red" | "yellow" | "blue";
type Player = {
healthBar: number;
team: Team;
};
const player: Player = {
healthBar: 10,
team: "blue",
};
- & ๋ก type์ ํ์ฅํ์ฌ ์๋ก์ด type์ ๋ง๋ค ์๋ ์๋ค.
type Animal = {
name: string;
};
type Bear = Animal & {
honey: boolean;
};
const bear = getBear();
bear.name;
bear.honey;
Abstract class vs Interface
๊ณตํต์
- ๋ ๋ค ํด๋์ค์ ๋ชจ์์ ์ ์ํ ์ ์๋ค.
์ฐจ์ด์
- Abstract class๋ ์ปดํ์ผํ๋ฉด ์ผ๋ฐ ํด๋์ค๊ฐ ๋๊ณ , Interface๋ ์ปดํ์ผํ๋ฉด ์ฌ๋ผ์ง๋ค.
- ๋ค์ค ์์์ ์ง์ํ์ง ์์ผ๋ฏ๋ก ํด๋์ค๋ ํ๋์ Abstract class๋ง ์์(extends)ํ ์ ์๊ณ ,
๋ค์ค ๊ตฌํ์ ์ง์ํ๋ฏ๋ก ์ฌ๋ฌ ๊ฐ์ interface๋ก ํ๋์ ํด๋์ค๋ฅผ ๊ตฌํ(implements)ํ ์ ์๋ค. - Abstract class๋ ์ถ์ ๋ฉ์๋๊ฐ ์๋ ์ผ๋ฐ ๋ฉ์๋์๋ ๊ตฌํ์ ํฌํจํ ์ ์์ง๋ง,
Interface๋ ๋ฉ์๋์ ๊ตฌํ์ ํฌํจํ ์ ์๊ณ call signature๋ง ์จ์ผ ํ๋ค.
๊ฒฐ๋ก
- ํด๋์ค์ ๋ชจ์์ ์ ์ํ ๋
๊ณตํต๋ ๊ธฐ๋ฅ์ ๊ฐ์ง๋๋ก ๊ตฌํ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค๋ฉด abstract class๋ฅผ ์ฌ์ฉํ๋ค.
๊ทธ๋ ์ง ์๋ค๋ฉด ์ปดํ์ผํ๋ฉด ์ฌ๋ผ์ง๋ interface๋ฅผ ์ฌ์ฉํ๊ธธ ๊ถ์ฅํ๋ค.
Interface vs Type
๊ณตํต์
- ๋ ๋ค ๊ฐ์ฒด์ ๋ชจ์์ ์ ์ํ ์ ์๋ค.
- ์ปดํ์ผํ๋ฉด ์ฌ๋ผ์ง๋ค.
- abstract class๋ฅผ ๋์ฒดํ ์ ์๋ค.
type PlayerA = {
firstName: string;
};
interface PlayerB {
firstName: string;
}
class User implements PlayerA {
constructor(public firstName: string) {}
}
// ๋๋
class User implements PlayerB {
constructor(public firstName: string) {}
}
์ฐจ์ด์
- ์ ์ํ๋ ๋ฐฉ์์ด ๋ค๋ฅด๋ค.
interface Y { }
type X = { }
- ํ์ฅ ๋ฐฉ์์ด ๋ค๋ฅด๋ค.
interface A extends B { }
type A = B & { }
- interface๋ ์ฌ๋ฌ ๋ฒ์ ์ ์ธ์ ๋ณํฉํ ์ ์์ง๋ง, type์ ์ ์ธ์ ๋ณํฉํ ์ ์๋ค.
- type์ ํน์ ๊ฐ์ผ๋ก ํ์ ์ ์ ํํ๋ ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ ์ ์์ด ํ์ฉ๋๊ฐ ์ข ๋ ๋๋ค.
๊ฒฐ๋ก
- interface๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๊ฐ๋ ์ ํ์ฉํด์ ๋์์ธ๋์๊ณ , type์ ์ข ๋ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
- ํด๋์ค๋ ๊ฐ์ฒด์ ๋ชจ์์ ์ ์ํ๊ณ ์ถ์ผ๋ฉด interface,
์ด์ธ์๋ ํ์ ๋ณ์นญ์ด๋ ํน์ ๊ฐ์ผ๋ก ํ์ ์ ์ ํํ๋ ๊ฒฝ์ฐ type ์ฌ์ฉ์ ๊ถ์ฅํ๋ค.
์ฐธ๊ณ
โDo it! ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐโ - ์ ์ํ
โํ์ ์คํฌ๋ฆฝํธ๋ก ๋ธ๋ก์ฒด์ธ ๋ง๋ค๊ธฐโ - Nomad Coders ๊ฐ์
๐ ํ์ ์คํฌ๋ฆฝํธ ํด๋์ค · ๊ฐ์ฒด ์งํฅ ๋ฌธ๋ฒ ๐ฏ ์ด์ ๋ฆฌ
private, protected ํ๋กํผํฐ์ ๋ฉ์๋
TypeScript - ์ธํฐํ์ด์ค vs ์ถ์ ํด๋์ค
Differences Between Type Aliases and Interfaces
'Language > TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TypeScript] ์ค์ฒฉ๋ ๊ฐ์ฒด์ ๊ฐ์ ํ์ ์ผ๋ก ๋ง๋ค๊ณ ์ถ์ ๋ (0) | 2024.12.31 |
---|---|
[TypeScript] Declaration File & JSDoc (1) | 2024.08.07 |
[TypeScript] TypeScript ํ์ ์ ๋ฆฌ (0) | 2024.08.07 |
[TypeScript] TypeScript ์๋์ผ๋ก ์ค์ ํ๊ธฐ (0) | 2024.08.07 |
[TypeScript] TypeScript ์ฅ์ ๊ณผ ํน์ง (0) | 2024.08.05 |