TypeScript - 인터페이스

Seung min, Yoo·2021년 5월 6일
0

1. 인터페이스 작성법

interface TV { //인터페이스는 행위(메서드)를 기술해야 한다.
  turnOn(): void;	//void는 기본적으로 아무것도 반환하지 않는다는 것이다.
  turnOff(): boolean;
}

const myTV: TV = {
  turnOn() {
    
  },
  turnOff() {
    return false;
  }
}

function tryTurnOn(tv: TV) {
  tv.turnOn(myTV);
}
tryTurnOn(myTv);

interface Cell {
  row: number;
  col: number;
  piece?: piece; // piece가 없을 수도 있기에 '?'를 주고 optional 하게 되는 것이다.
}

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 < 3; col++) {
      cells.push({ row, col })
    }
  }
  return cells;
}

const board = createBoard();
board[0].piece = {
  move(from:cell, to: cell) {
    return true;
  }
}
profile
이제 막 개발을 시작한 프로그래밍 입문자

0개의 댓글