A modern, lightweight TypeScript SDK starter template with all the best practices. Use this as a foundation for building your own TypeScript SDKs and libraries.
- 📦 tsup for fast, zero-config bundling
- ⚡️ Vitest for blazing fast testing with coverage support
- 📝 TypeScript strict mode enabled
- 🔨 ESLint + Prettier for code quality and formatting
- 📋 Example usage included
- 🚀 GitHub Actions CI with multi-Node version testing
- 📦 pnpm for fast, efficient package management
- 🎯 Dual package support (ESM + CommonJS)
- 🌳 Tree-shakeable with proper
sideEffectsconfiguration - ✅ Build verification to ensure all outputs are generated
- Click "Use this template" button on GitHub or clone this repo
- Find and replace
typescript-sdk-starterwith your package name - Update
package.json:- Change
name,description,author,repository,bugs, andhomepage - Update
keywordsto match your SDK's purpose
- Change
- Replace the example code in
src/index.tswith your SDK implementation - Update this README with your SDK's documentation
- Remove or update the example in
examples/basic.ts - Write tests in
tests/directory
# Install dependencies
pnpm install
# Run tests
pnpm test
# Run tests with coverage
pnpm test:coverage
# Build
pnpm build
# Development mode (watch)
pnpm dev
# Type check
pnpm typecheck
# Lint
pnpm lint
# Format code
pnpm formatOnce published, users can install your SDK:
# npm
npm install typescript-sdk-starter
# pnpm
pnpm add typescript-sdk-starter
# yarn
yarn add typescript-sdk-starterimport { SDK } from 'typescript-sdk-starter';
// Initialize with configuration
const sdk = new SDK({
apiKey: 'your-api-key',
baseURL: 'https://api.example.com', // optional
timeout: 5000, // optional, in milliseconds
});
// Fetch a user
const user = await sdk.getUser(1);
console.log(user);
// List all users
const users = await sdk.listUsers();
// Create a new user
const newUser = await sdk.createUser({
name: 'Jane Doe',
email: 'jane@example.com',
});
// Update a user
await sdk.updateUser(1, { name: 'Updated Name' });
// Delete a user
await sdk.deleteUser(1);import { SDK, SDKError } from 'typescript-sdk-starter';
try {
const user = await sdk.getUser(999);
} catch (error) {
if (error instanceof SDKError) {
console.error('SDK Error:', error.code, error.message);
if (error.statusCode) {
console.error('HTTP Status:', error.statusCode);
}
}
}// ESM
import { SDK } from 'typescript-sdk-starter';
// CommonJS
const { SDK } = require('typescript-sdk-starter');# Basic example
pnpm tsx examples/basic.ts
# Advanced example with error handling
pnpm tsx examples/advanced.tsMain SDK class for managing API interactions.
new SDK(config: SDKConfig)Parameters:
config.apiKey(string, required): Your API key for authenticationconfig.baseURL(string, optional): Base URL for API requestsconfig.timeout(number, optional): Request timeout in milliseconds (default: 5000)config.headers(object, optional): Custom headers to include in all requests
Returns the current SDK configuration.
Fetches a user by ID.
Fetches all users.
Creates a new user.
Updates an existing user.
Deletes a user by ID.
Custom error class for SDK-related errors.
Properties:
message(string): Error messagecode(string): Error code (e.g., 'API_ERROR', 'TIMEOUT', 'NETWORK_ERROR')statusCode(number, optional): HTTP status code when applicable
interface User {
id: number;
name: string;
email: string;
username?: string;
}interface APIResponse<T> {
data: T;
status: number;
headers: Record<string, string>;
}typescript-sdk-starter/
├── src/ # Source code
│ └── index.ts # Main entry point
├── tests/ # Test files (mirrors src/ structure)
│ └── index.test.ts
├── examples/ # Usage examples
│ ├── basic.ts # Simple usage
│ └── advanced.ts # Error handling & config
├── dist/ # Build output (auto-generated)
└── coverage/ # Coverage reports (auto-generated)
pnpm build- Build for production (CJS + ESM + type definitions)pnpm dev- Build in watch modepnpm test- Run tests oncepnpm test:watch- Run tests in watch modepnpm test:coverage- Run tests with coverage reportpnpm typecheck- Check TypeScript typespnpm lint- Lint codepnpm lint:fix- Fix lint issues automaticallypnpm format- Format code with Prettierpnpm clean- Remove build output
# Runtime dependencies
pnpm add package-name
# Development dependencies
pnpm add -D package-name- Update version in
package.json(follow semver) - Build and test:
pnpm clean pnpm build pnpm test pnpm typecheck pnpm lint - Publish to npm:
npm publish
Note: The
prepublishOnlyscript automatically runs the build before publishing.
- All tests passing
- Types are correct (
pnpm typecheck) - Code is linted and formatted
- Version bumped appropriately
- CHANGELOG updated (if you're maintaining one)
- README documentation is up to date
- Examples work with the new version
.prettierrc.json- Prettier formatting rulestsconfig.json- TypeScript compiler optionstsup.config.ts- Build configurationvitest.config.ts- Test configurationeslint.config.js- Linting rules.npmignore- Files to exclude from npm package
Problem: Build fails with module errors
# Clear cache and rebuild
rm -rf dist node_modules
pnpm install
pnpm buildProblem: Types not being generated
- Check
tsconfig.jsonhas"declaration": true - Check
tsup.config.tshasdts: true
Problem: Tests fail to import modules
- Ensure Vitest configuration matches your TypeScript setup
- Check that test files have
.test.tsextension
Problem: "Cannot find module" errors
- Verify
exportsfield inpackage.jsonis correct - Ensure both
dist/index.js(ESM) anddist/index.cjsexist after build - Check that
typesfield points to correct.d.tsfile
- Node.js >= 20.0.0
- pnpm >= 9.0.0
MIT © Dan Schoonmaker
See CONTRIBUTING.md for development guidelines.
- tsup - Bundle TypeScript libraries
- Vitest - Fast unit testing
- TypeScript - Typed JavaScript