Skip to content
Status
Active
Level
Intermediate
Updated
2026. 06. 22.

A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value

ini
A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.

"void", "undefined", "any"가 아닌 반환 타입을 선언한 함수가, 실제 코드 상에서 그 타입의 값을 끝까지 반환하지 않는다는 의미를 지닌 컴파일러 에러 메시지이다.

보통 함수를 작성 중에 IDE 상에 나타나 심기를 거스른다.

발생 상황: 함수 작성을 막 시작한 참

index.ts에서 새 기능을 추가하던 도중

typescript
/** 2026-06-22;
 * 1. photoswipe 초기 사진 크기를 뷰포트의 90%로 고정
 * 2. 모바일 환경의 photoswipe 패널 닫기 -> 빈 영역 터치
 * 3. Tag, photoswipe 뒤로가기 기능 오류 -> pushState로 직접 관리
 */
const LIGHTBOX_VIEWPORT_RATIO = 0.9;

function lightboxSize(img: HTMLImageElement): {width: number; height: number} {
  const maxW = window.innerWidth * LIGHTBOX_VIEWPORT_RATIO;
  const maxH = window.innerHeight * LIGHTBOX_VIEWPORT_RATIO;

  // 기준 ─ 종횡비: SVG는 intrinsic 픽셀이 0
  let rw = img.naturalWidth;
  let rh = img.naturalHeight;
  if(!rw || !rh) {
    const r = img.getBoundingClientRect();
    rw = r.width || 4;
    rh = r.height || 3;
  }
  //...

위 상황에서 함수 lightboxSize의 시그니처는 다음과 같다.

typescript
function lightboxSize(img: HTMLImageElement): {width: number; height: number} {

시그니처의 선언에 따르면, "이 함수는 반드시 number 타입의 두 변수 width, height를 가진 객체를 반환한다"는 계약을 맺고 있다.

그런데 함수의 return문을 만들어놓지 않은, 실제로는 계산만 시작한 미완성 상태이다..

TypeScript의 컴파일러는 임시 코드 단계라도 봐주지 않고 바로 TS2355 계열 에러를 반환한다.

그냥 참고 끝까지 작성하든가, 아래처럼 return 문을 미리 선언해두는 것도 방법이다.

typescript
function lightboxSize(img: HTMLImageElement): {width: number; height: number} {
  //... 아래처럼 return문을 미리 적어놓고 코딩을 시작한다.
  return {width: Number(0), height: Number(0)};
}