General
Code Style
- Use
camelCasefor all field names and variables. - Prefer named exports over default exports.
- Use TypeScript’s strict mode.
- Place types co-located with implementation files.
Naming Conventions
- Types/Interfaces: Use
PascalCase(e.g.,UserService,ClientForm). - Variables/Functions: Use
camelCase(e.g.,userService,getConfig). - Constants: Use
UPPER_SNAKE_CASEorcamelCasedepending on export type. - Files: Use
kebab-casefor directories,PascalCasefor components/classes,camelCasefor utilities. - React Components: Use
PascalCase(e.g.,ButtonCard.tsx). - Test files: End with
.test.tsfor unit tests,.int.spec.tsfor integration tests.
Type Imports
Use the type keyword for type-only imports to clearly distinguish types from values:
1import type { CollectionConfig, Config } from 'payload'
2import { cacheHookCollections } from './plugin/hooks.js'
3import type { PayloadHelperPluginConfig } from './types.js'
Documentation
- Document all exported functions with JSDoc comments.
- Explain the purpose and parameters of functions.
- Use
@paramand@returnstags for clarity.
Example:
1/**
2 * Generates an alphanumeric random string.
3 * @param {number} length - The length of the string to generate
4 * @returns {string} The generated random string
5 */
6export const generateRandomString = (length: number): string => {
7 let result = ''
8 while (result.length < length) {
9 result += (Math.random() + 1).toString(36).substring(2)
10 }
11 return result.substring(0, length)
12}
Utility Functions
- Write pure functions with no side effects.
- Follow single responsibility principle.
- Always provide type annotations on parameters and return types.