a dotfile but it's really big
TypeScript Best Practices#
Code Style#
- Use strict mode (
"strict": truein tsconfig.json) - Prefer
interfaceovertypefor object shapes - Use
constby default,letwhen needed, nevervar - Enable
noImplicitAnyandstrictNullChecks
Type Safety#
// GOOD: Explicit types and null handling
function getUser(id: string): User | undefined {
return users.get(id);
}
const user = getUser(id);
if (user) {
console.log(user.name); // TypeScript knows user is defined
}
// BAD: Type assertions to bypass safety
const user = getUser(id) as User; // Dangerous if undefined
console.log(user.name); // Might crash
Error Handling#
- Use try/catch for async operations
- Define custom error types for domain errors
- Never swallow errors silently
Security#
- Validate all user input at API boundaries
- Use parameterized queries for database operations
- Sanitize data before rendering in DOM (prevent XSS)
- Never use
eval()orFunction()with user input