타입스크립트로 작성된 오픈소스를 보면, 종종 다음과 같은 코드를 직면하게 되는 순간이 있다.
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가 등장하였다.
☁ 참고 자료
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
Documentation - TypeScript 3.8
TypeScript 3.8 Release Notes
www.typescriptlang.org
'TypeScript' 카테고리의 다른 글
Typescript에서 Decorators 사용하기 (0) | 2023.08.12 |
---|---|
typescript) 문자열 배열의 원소들의 값을 Union type으로 가지는 type 생성하기 (0) | 2023.08.04 |
Typescript에서 Error 처리하기 (0) | 2023.06.22 |
Typescript 제네릭(Generics) 사용하기 (0) | 2023.06.20 |
typescript에서 typeof (with keyof) (0) | 2023.06.10 |