website/src/pages/lint/rules/useNamingConvention.md
Enforce naming conventions for everything across a codebase.
Enforcing naming conventions helps to keep the codebase consistent, and reduces overhead when thinking about the name case of a variable.
All names can be prefixed and suffixed by underscores _ and dollar signs $.
All variables, including function parameters and catch parameters, are in camelCase.
Additionally, top-level variables declared as const or var may be in CONSTANT_CASE or PascalCase.
Top-level variables are declared at module or script level.
Variables declared in a TypeScript module or namespace are also considered top-level.
function f(param, _unusedParam) {
let localValue = 0;
try {
/* ... */
} catch (customError) {
/* ... */
}
}
export const A_CONSTANT = 5;
export const Person = class {}
let aVariable = 0;
export namespace ns {
export const ANOTHER_CONSTANT = "";
}
Examples of incorrect names:
let a_value = 0;
function f(FirstParam) {}
A function name is in camelCase or PascalCase.
function trimString(s) { /*...*/ }
function Component() {
return <div></div>;
}
enum namesA TypeScript enum name is in PascalCase.
enum members are by default in PascalCase.
However, you can configure the case of enum members.
See options for more details.
enum Status {
Open,
Close,
}
A class name is in PascalCase.
A static property name and a static getter name are in camelCase or CONSTANT_CASE.
A class property name and a class method name are in camelCase.
class Person {
static MAX_FRIEND_COUNT = 256;
static get SPECIAL_PERSON_INSTANCE() { /*...*/ }
initializedProperty = 0;
specialMethod() {}
}
type aliases and interfaceA type alias and an interface name are in PascalCase.
A property name and a method name in a type or interface are in camelCase or CONSTANT_CASE.
A readonly property name and a getter name can also be in CONSTANT_CASE.
type Named = {
readonly fullName: string;
specialMethod(): void;
};
interface Named {
readonly fullName: string;
specialMethod(): void;
}
interface PersonConstructor {
readonly MAX_FRIEND_COUNT: number;
get SPECIAL_PERSON_INSTANCE(): Person;
new(): Person;
}
Examples of an incorrect type alias:
type person = { fullName: string };
Literal object property and method names are in camelCase.
const alice = {
fullName: "Alice",
}
Example of an incorrect name:
const alice = {
FULL_NAME: "Alice",
}
Imported and exported module aliases are in camelCase.
import * as myLib from "my-lib";
export * as myLib from "my-lib";
import and export aliases are in camelCase, PascalCase, or CONSTANT_CASE:
import assert, {
deepStrictEqual as deepEqual,
AssertionError as AssertError
} from "node:assert";
Examples of an incorrect name:
import * as MyLib from "my-lib";
A TypeScript type parameter name is in PascalCase.
function id<Val>(value: Val): Val { /* ... */}
namespace namesA TypeScript namespace name is in camelCase or in PascalCase.
namespace mathExtra {
/*...*/
}
namespace MathExtra {
/*...*/
}
The rule provides two options that are detailed in the following subsections.
{
"//": "...",
"options": {
"strictCase": false,
"enumMemberCase": "CONSTANT_CASE"
}
}
When this option is set to true, it forbids consecutive uppercase characters in camelCase and PascalCase.
For instance, when the option is set to true, HTTPServer or aHTTPServer will throw an error.
These names should be renamed to HttpServer and aHttpServer
When the option is set to false, consecutive uppercase characters are allowed.
HTTPServer and aHTTPServer are so valid.
Default: true
By default, the rule enforces the naming convention followed by the TypeScript Compiler team:
an enum member has to be in PascalCase.
You can enforce another convention by setting enumMemberCase option.
The supported cases are: PascalCase, CONSTANT_CASE, and camelCase.