TypeScript

Type only import, export에 대해서

citron031 2023. 7. 5. 23:17

타입스크립트로 작성된 오픈소스를 보면, 종종 다음과 같은 코드를 직면하게 되는 순간이 있다.

import type { Test } from "./Test";

export type { Test };

해당 문법은 타입스크립트 버전 3.8에서 추가된 type only imports and exports 문법이다.

 

이는 타입만을 import하고 export하기에 타입스크립트에서 불필요하게 모듈을 불러오는 문제를 해결할 수 있다.

 

런타임 시 사용되지 않는 import를 구분할 수 있기에 컴파일 단계에서 사용되지 않는 import를 제거할 수 있다.

⭐️ 항상 완전히 지워지기 때문에, 런타임 실행 시 해당 코드는 남지 않는다.

 

때때로 타입에 대한 import/export가 제대로 지워지지 않아 오류가 발생하는 경우가 있었기에, type only import/export가 등장하였다.

 

☁ 참고 자료

https://stackoverflow.com/questions/61412000/do-i-need-to-use-the-import-type-feature-of-typescript-3-8-if-all-of-my-import

 

Do I need to use the "import type" feature of TypeScript 3.8 if all of my imports are from my own file?

I have a simple file types.ts that defines some types: export interface MyInterface { // ... } export const enum MyEnum { // ... } export type MyType = { // ... } I have read about the new

stackoverflow.com

 

https://www.typescriptlang.org/ko/docs/handbook/release-notes/typescript-3-8.html#%ED%83%80%EC%9E%85-%EC%A0%84%EC%9A%A9-imports-%EC%99%80-exports-type-only-imports-and-exports

 

Documentation - TypeScript 3.8

TypeScript 3.8 Release Notes

www.typescriptlang.org