Interfaces
- ์ธํฐํ์ด์ค๋ ์ต์ํ ํ์
๊ณผ ๋น์ทํ์ง๋ง, ๋ ๊ฐ์ง ๋ถ๋ถ์์ ๋ค๋ฅธ ์ ์ ์ดํดํด์ผ ํ๋ค.
- ์ธํฐํ์ด์ค๋ object์ ๋ชจ์์ ์๋ ค์ฃผ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
- ์ธํฐํ์ด์ค๋ ๋ํ property๋ฅผ ์ถ์ ํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
- ์ธํฐํ์ด์ค๋ ํด๋์ค์ ๋ชจ์์ ์๋ ค์ฃผ๊ณ , ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ์ปดํ์ผ๋์ง ์๋๋ค๋ ์ ์์ ์ ์ฉํ๋ค.
Type์ ๊ธฐ๋ฅ
- type์ด ํ์
์คํฌ๋ฆฝํธ์์ ์ผ๋ง๋ ์ ์ฉํ์ง๋ฅผ ์์์ผ ํ๋ค.
- Player๋ผ๋ ํ์
์์ ๋๋ค์๊ณผ ์ฒด๋ ฅ์ ๋ํด ๋ค๋ฅธ ํ์
์ ๋ถ์ฌํ ์ ์๋ค.
- ๋ํ Player๋ผ๋ ํ์
์ผ๋ก ๋๊ฐ์ ์๋ฏธ๋ก ์ฌ์ฉ๋๋ ๋ณ์ ์ฌ๋ฌ ๊ฐ๋ฅผ ๋ง๋ค ์ ์๋ค.
- object์ ๋ชจ์์ ์๋ ค์ฃผ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
type Player = {
nickname: string,
healthBar: number
}
const imae: Player = {
nickname: "imae",
healthBar: 100
}
- ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ Food๊ฐ string์ด์ด์ผ ํ๋ค๊ณ ์ ์ธํ ์ ์๋ค.
- ํ์
์ด string์ด๋ผ๊ณ ์๋ ค์ฃผ๋๋ฐ ์ฌ์ฉํ ์๋ ์๋ค.
type Food = string;
const kimchi:Food = "delicious"
- ํ์
alias (๋์ฒด๋ช
)๋ฅผ ์ธ ์๋ ์๋ค.
type Nickname = string
type Health = number
type Friends = Array<string>
- Team์๋ง ์๋ ์์ ์ฌ์ฉํ ์ ์๊ณ , Health์ ์๋ ์ซ์๋ง ์ฌ์ฉํ ์ ์๋ค.
type Team = "red" | "blue" | "yellow" // alias
type Health = 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100
type Player = {
nickname: string,
team: Team
health: Health
}
const imae: Player = {
nickname: "imae",
team: "red",
health: 100
}
Interface๋ก type์ ๋์ฒด
- type์ ์์์ ์ดํด๋ณธ ๊ฒ๊ณผ ๊ฐ์ด ๋ค์ํ ์ฉ๋๊ฐ ์๋ค.
- ํ์ง๋ง Interface๋ object์ ๋ชจ์์ ํน์ ํด์ฃผ๋ ์ญํ ๋ง ์กด์ฌํ๋ค.
- object์ ๋ชจ์์ ํน์ ํ๋ ๋ฐฉ๋ฒ์ด ์ฐ๋ฆฌ์๊ฒ 2๊ฐ๊ฐ ์๊ธด ๊ฒ์ด๋ค. type์ ์ฌ์ฉํ๊ฑฐ๋ Interface๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- ํ์ง๋ง type ํค์๋๊ฐ interface ํค์๋์ ๋นํด ์ฌ์ฉํ ์ ์๋ ์ฉ๋๋ ํจ์ฌ ๋ ๋ง๋ค.
type Team = "red" | "blue" | "yellow"
type Health = 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100
interface Player {
nickname: string,
team: Team
health: Health
}
const imae: Player = {
nickname: "imae",
team: "red",
health: 100
}
- ์๋์ ์์ ์ฉ๋๊ฐ ๊ฐ๋ค๊ณ ๋ณผ ์ ์๋ค.
- ํ์ง๋ง, ํ์๋ object์ ๋ชจ์์ ํน์ ํด์ฃผ๋ ์ญํ ์ด๋ผ๋ ๊ฒ์ ๊ฐ์กฐํ๋ ์๋ฏธ์์ interface๋ฅผ ์ฌ์ฉํ ๊ฒ์ด๋ค.
type imae = {
}
==
interface imae {
}
Interface ์์
- interface class1 extends class2
- class class1 extends class2์ ๊ตฌ๋ฌธ์ด ๋น์ทํ๋ค.
- extends ํค์๋๋ ํด๋์ค๋ฅผ ๋ค๋ฅธ ํด๋์ค์ ์์์ผ๋ก ๋ง๋ค๊ธฐ ์ํด class ์ ์ธ ๋๋ class ์์ ์ฌ์ฉ๋๋ค.
interface User {
name: string
}
interface Player extends User {
}
const imae: Player = {
name: "imae"
}
- ๋๊ฐ์ ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์๋ ์ฝ๋๋ฅผ type์ ํํ๋ก ๋ฐ๊พธ๋ฉด ์๋์ ๊ฐ๋ค.
- &๋ and๋ฅผ ์๋ฏธํ๋ค.
- |๋ or์ ์๋ฏธํ๋ค.
type User = {
name: string
}
type Player = User & {
}
const imae: Player = {
name: "imae"
}
Interface property ์ถ์
- ์๋ ์ฝ๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก User class๋ฅผ ํ์ฅ์ํฌ ์ ์๋ค.
interface User {
name: string
}
interface User {
lastName: string
}
interface User {
health: number
}
const imae: User = {
name: "imae",
lastName: "kwak",
health: 23
}
- type ํค์๋๋ ์ด์ ๊ฐ์ interface์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
type User {
name: string
}
type User {
lastName: string
}
type User {
health: number
}
const imae: User = {
name: "imae",
lastName: "kwak",
health: 23
}
์ถ์ํ๋ฅผ ์ํ ๋ Class์ Interface๋ฅผ ์ฌ์ฉํ ๋ ์ฐจ์ด์
- ์ถ์ ํด๋์ค๋ ์ ์ฉํ๋ฐ, ๋ค๋ฅธ ํด๋์ค๊ฐ ๊ฐ์ ธ์ผ ํ property๋ ๋ฉ์๋๋ฅผ ๋ช
์ํ ์ ์๋๋ก ๋์์ค๋ค.
- ์ถ์ ํด๋์ค๋ ๊ทธ๊ฒ์ผ๋ก๋ถํฐ ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ฑธ ํ์ฉํ์ง ์๋๋ค.
- ์ฆ, new User๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- ์์๋ฐ๋ ํด๋์ค๊ฐ ์ด๋ป๊ฒ ๋์ํด์ผํ ์ง ์๋ ค์ฃผ๊ธฐ ์ํด์ ์ถ์ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ค.
- ์ถ์ํด๋์ค์ ๋ฌธ์ ์ ์ ์๋ฐ์คํฌ๋ฆฝํธ์๋ abstract์ ๊ฐ๋
์ด ์๋ค๋ ๊ฒ์ด๋ค.
- ์ผ๋ฐ์ ์ธ class๋ผ๊ณ ์ธ์ํ๋ค.
- ์ธํฐํ์ด์ค๋ ์ปดํ์ผํ๋ฉด JS๋ก ๋ฐ๋์ง ์๊ณ ์ฌ๋ผ์ง๋ค.
- ์ธํฐํ์ด์ค๋ฅผ ์ธ ๋ ํด๋์ค๊ฐ ํน์ ํํ๋ฅผ ๋ฐ๋ฅด๋๋ก ์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์์ง ์๊ฐํด๋ณด์์ผ ํ๋ค.
์ถ์ ํด๋์ค๋ฅผ ์ฌ์ฉํ ์ฝ๋
abstract class User {
constructor(
protected firstName: string,
protected lastName: string
) {}
abstract sayHi(name:string):string
abstract fullName():string
}
class Player extends User {
fullName(){
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string){
return `Hello ${name}. My name is ${this.fullName}`
}
}
์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ ์ฝ๋
- extends ํค์๋๋ ์๋ฐ์คํฌ๋ฆฝํธ์๋ ์กด์ฌํ์ง๋ง, ์๋ฐ์คํฌ๋ฆฝํธ์ ์กด์ฌํ์ง ์๋ implements๋ฅผ ์ฌ์ฉํ ๊ฒ์ด๋ค.
- implements๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๋๊ฐ ๋ ๊ฐ๋ฒผ์์ง๊ณ , User ์ธํฐํ์ด์ค๋ฅผ ์ถ์ ํ ์๊ฐ ์๋๋ฐ, ์๋ํ๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ์๋ ์กด์ฌํ์ง ์๋ ํค์๋์ด๊ธฐ ๋๋ฌธ์ด๋ค.
- ์ธํฐํ์ด์ค๋ฅผ ์์ํ ๋๋ property๋ก private๋ก ๋ง๋ค์ง ๋ชปํ๋ค.
- ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์์ํ ๋๋ property๋ก protected๋ก ๋ง๋ค์ง๋ ๋ชปํ๋ค.
- ์ฆ, ์ธํฐํ์ด์ค๋ฅผ ์์ํ ๋๋ property๊ฐ public์ด์ด์ผ๋ง ํ๋ค.
- ์ธํฐํ์ด์ค๋ ํด๋์ค์ ๋ชจ์์ ์๋ ค์ฃผ๊ณ , ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ์ปดํ์ผ๋์ง ์๋๋ค๋ ์ ์์ ์ ์ฉํ๋ค.
- ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ฉด constructor๋ฅผ ์ฌ์ฉํ ํ์๋ ์๋ค.
- ๋ํ ํ๋ ์ด์์ ์ธํฐํ์ด์ค๋ฅผ ์์ํด์ค ์๋ ์๋ค.
- ํ ํด๋์ค์ ์ฌ๋ฌ ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ์์ํ ์ ์๋ค.
interface User {
firstName: string,
lastName: string
sayHi(name:string):string
fullName():string
}
interface Human {
health: number
}
class Player implements User, Human {
constructor(
public firstName: string,
public lastName: string,
public health: number
){}
fullName(){
return `${this.firstName} ${this.lastName}`
}
sayHi(name:string){
return `Hello ${name}. My name is ${this.fullName}`
}
}
์ธํฐํ์ด์ค๋ฅผ ํ์
์ผ๋ก ์ง์
- ์ธํฐํ์ด์ค๋ฅผ ํ์
์ผ๋ก ์ง์ ํ ์ ์๋ค.
function makeUser(user: User) {
return "hi"
}
makeUser({
firstName: "kwak",
lastName: "imae",
fullName: () => "xx",
sayHi: () => "string"
})
์ธํฐํ์ด์ค์ ํ์
์ ์ฐจ์ด์ : ์ฝ๋ ์ฌ์ฉ๋ฒ
ํ์
์ผ๋ก ์์ฑํ ์ฝ๋
- ํ์
์ ๋๊ฐ์ด ์ถ๊ฐํ ์ ์๋ค. ์ด๋ฏธ ์ ์๋์ด ์์ด์ ์ค๋ณต๋๊ธฐ ๋๋ฌธ์ด๋ค.
type PlayerA = {
name: string
}
type PlayerAA = PlayerA & {
lastName: string
}
type PlayerAA = {
health: number
}
const playerA: PlayerAA = {
name: "imae",
lastName: "kwak"
}
์ธํฐํ์ด์ค๋ก ์์ฑํ ์ฝ๋
interface PlayerB {
name: string
}
interface PlayerBB extends PlayerB {
lastName: string
}
interface PlayerBB {
health:number
}
const playerB: PlayerBB = {
name: "imae",
lastName: "kwak",
health: 100
}
ํ์
๊ณผ ์ธํฐํ์ด์ค์ ์ฐจ์ด์ : ์์๊ณผ ์ถ์ํด๋์ค ๋์ฒด
ํ์
์ผ๋ก ์์ฑํ ์ฝ๋
type PlayerA = {
firstName:string
}
class User implements PlayerA {
constructor(
public firstName: string
){}
}
์ธํฐํ์ด์ค๋ก ์์ฑํ ์ฝ๋
interface PlayerB {
firstName: string
}
class User implements PlayerB {
constructor(
public firstName: string
){}
}
ํ์
๊ณผ ์ธํฐํ์ด์ค์ ์ฐจ์ด์ : TypeScript ๊ณต์ ๋ฌธ์
- ์ธํฐํ์ด์ค๋ก ์์ฑํ ์ฝ๋Type Aliases๊ณผ ์ธํฐํ์ด์ค๋ ๋งค์ฐ ์ ์ฌํ๋ฉฐ ๋ง์ ๊ฒฝ์ฐ ์์ ๋กญ๊ฒ ์ ํํ ์ ์๋ค.
- ์ธํฐํ์ด์ค์ ๊ฑฐ์ ๋ชจ๋ ๊ธฐ๋ฅ์ type์์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ฃผ์ ์ฐจ์ด์ ์ type์ ๋ค์ ์ด์ด ์ ์์ฑ์ ์ถ๊ฐํ ์ ์๋ ๊ฒ์ด๋ค.
- ๋ฐ๋ฉด ์ธํฐํ์ด์ค๋ ํญ์ ํ์ฅ ๊ฐ๋ฅํ๋ค.