Expert guidance for building production-ready TypeScript libraries with strict typing, tree-shaking support, and comprehensive testing. Follows modern library development best practices.
Expert assistant for building production-ready TypeScript libraries with modern tooling, strict type safety, and best practices for library authoring.
This skill provides guidance for developing TypeScript libraries using a battle-tested template structure. It emphasizes strict typing, modular exports for tree-shaking, comprehensive testing with Vitest, and professional code quality standards.
When helping with TypeScript library development, follow these guidelines strictly:
1. **Strict Typing**
- Enforce strict TypeScript typing throughout the codebase
- Avoid `any` type unless absolutely necessary and documented
- Provide explicit return types for all public functions
- Use proper type inference where appropriate
- Ensure full type coverage for exported APIs
2. **Module Structure**
- Place all library source files under `lib/` directory, not in the root
- Use named exports exclusively for all components and utilities
- Keep exports modular and side-effect free to support tree-shaking
- Avoid circular dependencies between modules
1. **Documentation**
- Write clear JSDoc comments for all public components, functions, and utilities
- Include parameter descriptions, return types, and usage examples
- Document edge cases and potential errors
2. **Code Style**
- Use `eslint` for linting and enforce consistent code style
- Use `prettier` for automatic code formatting
- Follow the project's configured style rules
- Write maintainable, self-documenting code with clear variable and function names
3. **Error Handling**
- Implement comprehensive error handling for all public APIs
- Provide clear, actionable error messages
- Document potential errors in JSDoc comments
- Follow security best practices for input validation
1. **Test Framework**
- Write tests using Vitest
- Mock DOM dependencies where needed for browser-based functionality
- Aim for high test coverage of public APIs
- Include edge cases and error scenarios in test suites
2. **Test Organization**
- Co-locate tests with source files or use a `__tests__` directory
- Use descriptive test names that explain the behavior being tested
- Group related tests using `describe` blocks
When implementing or checking code, use these npm scripts:
1. **Before Committing**
- Run `npm run format:fix` to ensure consistent formatting
- Run `npm run lint:fix` to catch and fix style issues
- Run `npm run test` to ensure all tests pass
- Run `npm run knip` to detect unused code
2. **Code Review Checklist**
- All exports are named (no default exports)
- Type safety is maintained (minimal use of `any`)
- JSDoc comments are present for public APIs
- Tests cover new functionality
- No side effects in module initialization
- Tree-shaking compatibility is maintained
```typescript
/**
* Utility function to safely parse JSON with type checking
* @param input - JSON string to parse
* @returns Parsed object or null if invalid
* @throws {TypeError} If input is not a string
*/
export function safeParse<T>(input: string): T | null {
if (typeof input !== 'string') {
throw new TypeError('Input must be a string');
}
try {
return JSON.parse(input) as T;
} catch {
return null;
}
}
```
```typescript
import { describe, it, expect } from 'vitest';
import { safeParse } from './parser';
describe('safeParse', () => {
it('should parse valid JSON', () => {
const result = safeParse<{ name: string }>('{"name":"test"}');
expect(result).toEqual({ name: 'test' });
});
it('should return null for invalid JSON', () => {
const result = safeParse('{invalid}');
expect(result).toBeNull();
});
});
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/typescript-library-development-template/raw