import type 引入的内容只在编译期存在,编译后整条导入语句被完全移除。当你只需要某个模块的类型而不调用它的值时用它最合适:import type { ApiData } from './api',避免把模块无谓打进产物造成体积浪费。Babel、Vite 等工具都认这个语法,已成为新项目的默认写法。
它还能逐个标记:import { type Props, fetchUser } from './lib' 只把 Props 当类型。开启 isolatedModules 时重导出类型必须用 export type。对只需要类型而模块本身有副作用的场景,import type 也能明确表达意图,避免打包器把多余代码带入产物。
import type { ApiData } from './api'; // 编译后整句消失 import { type Props, fetchUser } from './user'; function parse(raw: unknown): ApiData { return raw as ApiData; } // isolatedModules 开启时重导出类型必须: // export type { Props };