| name | typescript-conventions |
| description | TypeScript 5.x / JavaScript ES2022+ coding conventions. Use when writing or reviewing TypeScript/JavaScript code. |
TypeScript Conventions
Key decisions and patterns for TypeScript 5.x / ES2022+.
Naming
| Type | Convention | Example |
|---|
| Classes/Interfaces/Types | PascalCase | UserService, User |
| Variables/Functions | camelCase | userName, getUsers |
| Constants | SCREAMING_SNAKE_CASE | MAX_RETRIES |
| Files | camelCase.ts | userService.ts |
| React Components | PascalCase.tsx | UserProfile.tsx |
Key Decisions
interface User {
id: string;
name: string;
}
type Status = 'active' | 'inactive';
function parse(data: unknown): User {
if (isUser(data)) return data;
throw new Error('Invalid');
}
function isUser(obj: unknown): obj is User {
return typeof obj === 'object' && obj !== null && 'id' in obj;
}
Modern Features
const name = user?.profile?.name ?? 'Anonymous';
const count = config.count ?? 10;
async function fetchUser(id: string): Promise<User> {
const res = await fetch(`/users/${id}`);
if (!res.ok) throw new Error('Not found');
return res.json();
}
Common Pitfalls
function process(data: any) { }
function process(data: unknown) {
if (isUser(data)) { }
}
const user = data as User;
if (isUser(data)) { }
Tools
- Format: Prettier
- Lint: ESLint + typescript-eslint
- Test: Vitest (new) or Jest
Testing Pattern
describe('UserService', () => {
it('should return user when found', async () => {
const service = new UserService(mockRepo);
const result = await service.findById('1');
expect(result).toEqual({ id: '1', name: 'Test' });
});
});