模块系统与类型导出
约 225 字小于 1 分钟
布欧-Lewyon
2026-05-16
首页 › TypeScript › 模块与声明文件 › 模块系统与类型导出
类型导出/导入
// types.ts
export interface User {
name: string;
age: number;
}
export type Status = 'active' | 'inactive';
// 内联导出
export const createUser = (name: string): User => ({ name, age: 0 });// app.ts
import { User, Status } from './types';
// 仅类型导入(编译时可移除)
import type { User } from './types';import type / export type
// 编译时完全移除(不产生运行时依赖)
import type { User } from './types';
// 重导出类型
export type { User };
export { type User, createUser }; // 混合导出namespace
// 全局命名空间(适用于非模块化场景)
namespace Utils {
export function log(msg: string) { console.log(msg); }
export const VERSION = '1.0';
}
Utils.log('hello');小结
| 语法 | 作用 |
|---|---|
export / import | 标准 ES 模块 |
import type | 仅导入类型(编译时移除) |
export type | 仅导出类型 |
namespace | 非模块化的全局空间 |
