Follow consistent naming conventions
LOW1.0.0
When to apply Before naming or reviewing names of TypeScript variables, constants, functions, types, generic parameters, React components, props, event handlers, or Hooks.
Name identifiers by their kind, using one convention per kind across the codebase.
Implementation
- Variables and functions: camelCase, such as
productsFilteredandformatCurrency. - Booleans: a question prefix, such as
isDisabled,hasProduct, orcanEdit. - Module-level constants: UPPER_SNAKE_CASE, such as
FEATURED_PRODUCT_ID, includingas constobjects and arrays. - Types: PascalCase, such as
OrderStatus. - Generic parameters:
Tfollowed by a descriptive name, such asTRequest, when a function has more than one parameter or the meaning is not obvious; a singleTis fine for a simple generic. - Acronyms: treat them as words, such as
generateUserUrlandFaqItem, and avoid abbreviations that are not widely known. - React: PascalCase components;
ComponentNamePropsfor prop types;on*for callback props andhandle*for the functions that implement them;[value, setValue]foruseState. - Enforce what tooling can check with
@typescript-eslint/naming-convention,react/jsx-handler-names, andreact/hook-use-state.
This is a set of conventions; a project may choose different ones, as long as each kind has one.
Rationale
Consistent names let readers tell at a glance what an identifier is: a value, a type, a constant, or an event handler.
Prefixed generic names also avoid shadowing real types: in <Request extends Request>, the parameter refers to itself and the declaration does not compile.
Examples
Incorrect (counterexample):
type FAQItem = { question: string };
const generateUserURL = (id: string) => `/users/${id}`;
const handle = <Request extends Request>(req: Request) => {};
const [userName, setUser] = useState('');
<Button click={actionClick} />;
Correct:
type FaqItem = { question: string };
const generateUserUrl = (id: string) => `/users/${id}`;
const handle = <TRequest extends Request>(req: TRequest) => {};
const [userName, setUserName] = useState('');
<Button onClick={handleClick} />;
Validation
Run the naming lint rules, and review new identifiers against the conventions above.
A name required by an external API or framework, such as a library's option key, is not a violation.