命名空间
一句话解释
命名空间是 TypeScript construct (largely superseded 通过 ES 模块) 那个分组 related declarations under 单个 name 到 avoid 全局作用域 pollution.
用大白话说
命名空间分组 related code under named 容器. 你 declare 它们使用 namespace Name { ... } and access 成员使用点 notation: Name.function().
Namespaces were the original TypeScript module system before ES modules became standard. They are now largely superseded by ES modules (导入/导出), but still appear in declaration files and legacy code.
For new code, prefer ES modules. Use namespaces only when working with legacy code or when you need to group types and values in a single declaration file without module boundaries.
示例
typescript
namespace Geometry {
export const PI = 3.14159;
export function area(r: number) { return PI * r * r; }
}
console.log(Geometry.area(5)); // 78.54Geometry 命名空间分组 PI and 区域. 仅 exported 成员是 accessible 之外.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。