2.6 KiB
2.6 KiB
Shared TypeScript Types
This package contains shared TypeScript type definitions used across multiple applications.
Setup
1. Initialize the package
npm install
npm run build
2. Push to Git
git init
git add .
git commit -m "Initial commit: shared types package"
git remote add origin <your-git-repo-url>
git push -u origin main
Usage in Other Projects
Option 1: Install from Git (HTTPS)
Add to your project's package.json:
{
"dependencies": {
"@your-org/shared-types": "git+https://github.com/your-org/shared-types.git"
}
}
Or install via command:
npm install git+https://github.com/your-org/shared-types.git
Option 2: Install from Git (SSH)
{
"dependencies": {
"@your-org/shared-types": "git+ssh://git@github.com:your-org/shared-types.git"
}
}
Option 3: Install specific branch or tag
{
"dependencies": {
"@your-org/shared-types": "git+https://github.com/your-org/shared-types.git#v1.0.0"
}
}
Or for a branch:
{
"dependencies": {
"@your-org/shared-types": "git+https://github.com/your-org/shared-types.git#develop"
}
}
Using the Types
import { User, ApiResponse, Status } from '@your-org/shared-types';
const user: User = {
id: '123',
email: 'user@example.com',
name: 'John Doe',
createdAt: new Date(),
updatedAt: new Date()
};
const response: ApiResponse<User> = {
success: true,
data: user,
message: 'User retrieved successfully'
};
const status = Status.Active;
Updating the Package
When you make changes to the types:
- Update the version in
package.json - Build the package:
npm run build - Commit and push changes
- Update consuming applications:
npm update @your-org/shared-types
Development Workflow
Adding New Types
- Create new
.tsfiles in thesrc/directory - Export types from
src/index.ts - Build and test:
npm run build - Commit changes and push
Versioning
Follow semantic versioning:
- MAJOR: Breaking changes
- MINOR: New features, backward compatible
- PATCH: Bug fixes, backward compatible
Best Practices
- Keep types focused and modular
- Use clear, descriptive names
- Document complex types with JSDoc comments
- Avoid coupling to specific implementations
- Export everything through
index.ts
Structure
shared-types/
├── src/
│ ├── index.ts # Main export file
│ └── types.ts # Type definitions
├── dist/ # Compiled output (generated)
├── package.json
├── tsconfig.json
├── .gitignore
└── README.md