import type으로 type 가져오기

GwangSoo·2025년 9월 19일
1

개인공부

목록 보기
31/34
post-thumbnail

TypeScript에서 타입을 불러오는 방법은 두 가지가 있다.

import type { Foo } from "./foo";
// OR
import { type Foo } from "./foo";

단순 import를 이용해서 가져와도 되지만 위 구문을 사용했을 때 얻는 것이 무엇이 있나 찾아보았다.

import type

import type은 타입 전용 import 문법이다. 여기서 가져온 심볼은 타입 문맥에서만 쓸 수 있고 컴파일 결과물에는 항상 제거된다.

// 컴파일 전
import type { Foo } from "./foo";

function f(x: Foo) {
  return x;
}

// 컴파일 후
function f(x) {
  return x;
}

주의할 점

  1. 값으로는 사용할 수 없음

    클래스, 함수, 객체 등은 런타임에 실제 값이 필요하다. 하지만 import type은 빌드 시 제거되므로 값으로 사용할 수 없다.

    import type { Component } from "react";
    interface ButtonProps {
        // ...
    }
    class Button extends Component<ButtonProps> {
        //               ~~~~~~~~~
        // error! 'Component' only refers to a type, but is being used as a value here.
        // ...
    }

    이 경우엔 import { Component } from "react";처럼 일반 import가 필요하다.

  2. default import와 혼합 불가

    import type 구문은 default import와 named import를 동시에 쓸 수 없다. 이는 TS가 “무엇이 타입인지” 판별하기 어렵기 때문이다.

    import type Foo, { Bar, Baz } from "some-module";
    //     ~~~~~~~~~~~~~~~~~~~~~~
    // error! A type-only import can specify a default import or named bindings, but not both.

장점

  • 항상 제거됨: JS 결과물에 남지 않아 번들 사이즈가 줄어듬
  • 의도 명확: “타입 전용”임이 코드에 드러나 협업과 코드 리뷰가 쉬워짐
  • 규칙화 가능: ESLint/TSConfig와 함께 쓰면 일관된 코드 스타일 유지

마무리하며

Typescript 5.0 이후로 verbatimModuleSyntax라는 플래그가 나왔는데, 이를 eslint의 @typescript-eslint/consistent-type-imports와 함께 적절하게 잘 섞어서 사용해봐야겠다.

참고

0개의 댓글