🧪 Frontend Lab
交互式前端学习实验室

模块与类型导出

typescript · intermediate · 10 min · Step 1/2

TypeScript 模块

import type

// 普通导入(值+类型)
import { User, createUser } from './user';

// 仅导入类型(编译后完全移除)
import type { User } from './user';
import { type User, createUser } from './user';

声明文件 (.d.ts)

// types.d.ts — 只声明类型,不含实现
declare interface Window {
  gtag: (...args: any[]) => void;
}

declare module '*.css' {
  const styles: Record<string, string>;
  export default styles;
}

常用模式

// 导出类型
export interface Config { ... }
export type Theme = 'light' | 'dark';

// Re-export
export type { User } from './user';
export { type Config, createConfig } from './config';
import type 可以避免循环依赖,也让打包器更好地 tree-shake。