Write clear, predictable TypeScript and Vue TypeScript code with strong typing, maintainability, and consistent documentation conventions.
You write TypeScript code that is clear, predictable, and easy to maintain. The goal is to make the codebase safer, more understandable, and easier to refactor without over-engineering.
name.test.ts.Good:
/**
* We load the user here to make sure we have fresh data when the component mounts.
* Without this, the user info could be stale.
*/
Bad:
/**
* Load user.
*/
Avoid contractions in comments. Use do not instead of don't, it is instead of it's, etc. This makes comments easier to read, especially for non-native speakers.
If you use contractions, make sure they have proper apostrophes. Sometimes contractions can make a comment more approachable. If you choose to use them, use proper punctuation.
Comment on types when their purpose isn't obvious. If a type models an external API, or has a non-obvious constraint, explain it.
Explain relationships between types when they're not clear.
Example:
/**
* Maps UserStatus to a badge color used in the UI.
* Should stay in sync with the theme color palette.
*/
export type StatusColorMap = {
active: 'green'
inactive: 'gray'
}
Example:
/**
* Represents a partial object where at least one property is required.
* Useful when you want to enforce at least one field update in a PATCH request.
*/
export type AtLeastOne<T> = {
[K in keyof T]: Partial<T> & Pick<T, K>
}[keyof T]
Example:
/**
* useUser composable for loading and managing user data.
* Fetches the user from the API and exposes reactive state.
*
* Returns:
* - user: Ref<User | null>
* - isLoading: Ref<boolean>
* - loadUser: Function to manually trigger user loading
*/
export function useUser() { ... }
Example:
/**
* TODO: Replace with dynamic permissions from backend when available.
*/
export type Permissions = 'read' | 'write' | 'admin'
/**
* A user in the system.
* This type represents the internal data structure for application logic.
* If you need to expose user data publicly, use `PublicUser`.
*/
export type User = {
/** Unique identifier for the user (UUID). */
id: string
/** The user's full name. */
name: string
/** Email address. Must be validated before saving. */
email: string
/** ISO date string of when the user signed up. */
createdAt: string
/** Whether the user has verified their email. */
isVerified: boolean
}