문자열 시그니처

작업 중 서버에서 내려오는 값 중, 4가지 타입으로 이뤄진 키값에 대해 인터페이스를 선언해야 했는데, 문자열 시그니처를 이용하면 해결할 수 있었다.

interface SquareConfig {
  color?: string;
  width?: number;
}

SquareCondig가 color, width 프로퍼티 외에 또다른 속성이 있는 경우 위와 같이 정의. 여튼 color, width가 아닌 다른 프로퍼티 타입은 문제가 안됨.

function createSquare(config: SquareConfig): { color: string; area: number } {
  // ...
}
let squareOptions = { colour: "red", width: 100 };
let mySquare = createSquare(squareOptions);

Last updated

Was this helpful?