typescript-dev
TypeScript development best practices, code quality tools, and documentation templates. Activated when working with .ts, .tsx files or TypeScript projects.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
TypeScript Development Expert
This skill supports TypeScript project development.
🎯 Core Rules
Package Management
-
Required: Use
pnpmas package manager - Do not use
npmoryarn
Type Safety
-
tsconfig.json:
strict: truerequired -
Null Handling: Leverage optional chaining
?.and nullish coalescing?? -
Imports: Use ES modules, avoid
require() -
NO ANY: Do not use
anytype in production code
Best Practices
- Type Inference: Let TypeScript infer when obvious
- Generics: Use for reusable components
- Union Types: Prefer union types over enums for string literals
- Utility Types: Leverage built-in types (Partial, Pick, Omit)
Documentation
- Required: Use TSDoc format for documentation comments
- Public APIs only: Document exported functions, classes, and interfaces
- Self-documenting code: Prefer clear naming over excessive comments
- Document when necessary: Add TSDoc only when the code intent isn't obvious from the signature
🛠️ Code Quality Tools
Development Workflow
# Format code
pnpm run format
# Run linter
pnpm run lint
# Type check
pnpm tsc --noEmit
# Run tests with coverage
pnpm test -- --coverage
🎯 Quality Checklist
Check these during code review:
- Public APIs have TSDoc comments (when intent isn't clear from signature)
- No
anytype usage - Proper error handling
- Test coverage above 80%
- Type inference properly leveraged
- Utility Types utilized
- Optional chaining (
?.) and Nullish coalescing (??) used - ES modules used (avoid
require())
🚀 Common Patterns
Error Handling
// Good: Clear error types
class ValidationError extends Error {
constructor(message: string, public field: string) {
super(message);
this.name = 'ValidationError';
}
}
// Good: Result type pattern
type Result<T, E = Error> =
| { success: true; data: T }
| { success: false; error: E };
Async/Await
// Good: With error handling
async function fetchUserData(id: string): Promise<Result<UserData>> {
try {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
return { success: true, data };
} catch (error) {
return { success: false, error: error as Error };
}
}
Type Guards
// Good: Custom type guard
function isUserProfile(value: unknown): value is UserProfile {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
'username' in value
);
}
💡 Performance Tips
-
Avoid unnecessary re-renders (React)
- Use
React.memofor expensive components - Use
useMemo/useCallbackappropriately
- Use
-
Lazy Loading
- Dynamic imports for code splitting
-
React.lazy()for components
-
Type-only imports
import type { UserProfile } from './types';
🔍 Common Anti-patterns to Avoid
❌ Don't:
// Using any type
function process(data: any) { }
// Implicit any
function getValue(obj, key) { }
// Excessive type assertions
const user = data as User;
✅ Do:
// Proper type definitions
function process(data: UserData) { }
// Explicit types
function getValue<T>(obj: T, key: keyof T) { }
// Use type guards
if (isUser(data)) {
// data is User here
}
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-28
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!