export-public-types
Export All Types That Appear in Public APIs
Overview
When you publish a library, users need access to all types that appear in your public API. If a function returns User but User isn't exported, users can't type their own variables to match. Export all types that appear in public function signatures, return types, or interfaces.
This is essential for good developer experience in libraries.
When to Use This Skill
- Publishing TypeScript libraries
- Types appear in public function signatures
- Users need to reference your types
- Building reusable components
- Designing library interfaces
The Iron Rule
Export every type that appears in your public API. Users need these types to work with your library effectively.
Example
// 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');
Reference
- Effective TypeScript, 2nd Edition by Dan Vanderkam
- Item 67: Export All Types That Appear in Public APIs
More from marius-townhouse/effective-typescript-skills
precise-any-variants
Use when forced to use any. Use when any is too broad. Use when function types need any.
86narrow-any-scope
Use when any is unavoidable. Use when working with untyped libraries. Use when silencing specific type errors.
35tsdoc-comments
Use when documenting public APIs. Use when writing library code. Use when using JSDoc-style comments. Use when generating documentation. Use when explaining complex types.
33exhaustiveness-checking
Use when handling tagged unions. Use when adding new cases to discriminated unions. Use when switch statements must cover all cases.
13code-gen-independent
Use when confused about types at runtime. Use when trying to use instanceof with interfaces. Use when type errors don't prevent JavaScript output.
12tsconfig-options
Use when setting up a TypeScript project. Use when confused by type checking behavior. Use when strict mode causes unexpected errors.
11