关于 export-public-types
面向设计公共 API 的 TypeScript 库作者,说明为何应导出公共签名和返回值中使用的所有类型,方便库用户引用。
主要功能
- 01公共 API 类型导出指导
- 02函数签名中的类型
- 03返回值中的类型
- 04库接口设计
适用场景
- 01发布 TypeScript 库
- 02公开函数所用的类型
- 03设计可复用组件接口
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ marius-townhouse/ effective-typescript-skills --skill export-public-types安装后在 Claude Code 中输入/export-public-types确认已加载。
详细介绍
导出所有出现在公共 API 中的类型
概述
发布库时,用户需要能够访问所有出现在公共 API 中的类型。如果某个函数返回 User,但 User 未导出,用户就无法声明类型匹配的变量。请导出所有出现在公共函数签名、返回类型或接口中的类型。
这对提供良好的库开发体验至关重要。
何时使用此技能
- 发布 TypeScript 库
- 类型出现在公共函数签名中
- 用户需要引用你的类型
- 构建可复用组件
- 设计库接口
铁律
导出所有出现在公共 API 中的类型。用户需要这些类型才能有效使用你的库。
示例
// BAD: Internal type not exported
interface User {
id: string;
name: string;
}
export function getUser(id: string): User {
// ...
}
// User can't do:
// import { getUser, User } from 'library'; // Error: User not exported
// const user: User = getUser('123');
// GOOD: Export the type
export interface User {
id: string;
name: string;
}
export function getUser(id: string): User {
// ...
}
// User can now:
// import { getUser, User } from 'library';
// const user: User = getUser('123');
参考资料
- Dan Vanderkam 著《Effective TypeScript》第 2 版
- 条目 67:导出所有出现在公共 API 中的类型
评论
支持 Markdown