General

Code Style

  • Use camelCase for 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_CASE or camelCase depending on export type.
  • Files: Use kebab-case for directories, PascalCase for components/classes, camelCase for utilities.
  • React Components: Use PascalCase (e.g., ButtonCard.tsx).
  • Test files: End with .test.ts for unit tests, .int.spec.ts for 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 @param and @returns tags 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.
Last modified: 27/10/2025 2022-2025 ©ainsley.dev, All rights reserved.