개발/Typescript

interface

반응형
//interface는 빌드하면 지워지기 때문에 아무리 많아도 상관없다. 데이터 validation만 한다

interface TV{
    turnOn():void
    turnOff():boolean
}

const myTV:TV ={
    turnOn(){

    },
    turnOff(){
        return true
    }
}

function tryTurnOn(tv:TV){
    tv.turnOn()
}

tryTurnOn(myTV)

//? 물음표는 변수가 없어도 동작하도록 해줌
interface Cell{
    row:number
    col:number
    piece?:Piece
}

interface Piece{
    move(from:Cell,to:Cell):boolean
}

function createBoard(){
    const cells:Cell[]=[]
    for (let row = 0; row < 4; row++) {
        for (let col = 0; col < 4; col++) {
            cells.push({
                row,
                col
            })
        }
    }
    return cells
}

const board = createBoard()
board[0].piece={
    move(from:Cell,to:Cell){
        return true;
    }
}
반응형

'개발 > Typescript' 카테고리의 다른 글

class2  (0) 2021.04.16
class  (0) 2021.04.16
enum  (0) 2021.04.16
function  (0) 2021.04.16
basic type  (0) 2021.04.16