Constant Rules
All rules regarding readonly constant files
Rules that govern how readonly constant files are written. Every rule is enabled
by the recommended config and applies to .ts / .tsx files inside a
/constants/ folder of your project.
constant-naming-convention
Require readonly constant names to use UPPERCASE_SNAKE_CASE — uppercase
letters, digits, and underscores, with an uppercase first character.
Do
const API = {
path: '/api/v1/',
port: '4000'
}
export default APIDon't
const apiConfig = {
path: '/api/v1/',
port: '4000'
}
export default apiConfigThe rule only checks top-level const declarations in /constants/ files, and
it focuses on the default-exported constant (or the only one if there is no
default export yet).
constant-single-object
Require constant files to group values into a single top-level object.
Do
const API = {
path: '/api/v1/',
port: '4000'
}
export default APIDon't
const API_PATH = '/api/v1/'
export default API_PATHA single object makes the default export obvious and keeps related values together. Primitive constants or multiple top-level objects in the same file are rejected.
The rule checks for one and only one top-level const whose value is an
object expression. If you find yourself needing two constants, group them into
one object with two keys.
constant-filename-matches-name
Require constant file names to match constant names (excluding extension).
Do
const API = {
path: '/api/v1/',
port: '4000'
}
export default APIDon't
const API_CONFIG = {
path: '/api/v1/',
port: '4000'
}
export default API_CONFIGA matching file name keeps the import path and the constant name in lockstep, which matters a lot for constants that are used in many places.
constant-require-default-export
Require constant files to default export their constants object.
Do
const API = {
path: '/api/v1/',
port: '4000'
}
export default APIDon't
const API = {
path: '/api/v1/',
port: '4000'
}
export { API }The equivalent export { Name as default } form is also accepted:
const API = {
path: '/api/v1/',
port: '4000'
}
export { API as default }A default export lets the importer rename the constant to fit the surrounding
context (e.g. API as api) without having to know the original file-level
export name. It also keeps the import side tidy: one symbol in, one symbol
out.