Skip to content

Good code is its own best documentation. β€” Steve McConnell

Enforce variable names to include their type (declare-keyword) ​

πŸ’Ό This rule is enabled in the following configs: 🌐 all, βœ… recommended.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

πŸ“– Rule details ​

Don't you hate it when you're unsure whether you can mutate a variable because it's declared with const, or if you're dealing with a class that can be instantiated?

By prefixing the variable name with its type, you can easily avoid these confusions and have a clearer understanding of your code at a glance.

βš™οΈ Options ​

side ​

(default: natural)

  • start - the type must be at the start of the variable name
  • end - the type must be at the end of the variable name
  • natural - the type must be at the natural position of the variable name (start for consts, end for classes, etc.)

oneLetter ​

(default: false)

If true, the type must be a single letter (e.g. cName for a const, eName for an enum, etc.). Classes get k, not to be confused with consts.

πŸ”§ Config ​

js
{ rules: { 'ninja/declare-keyword': [2, { side: 'start', oneLetter: false }] } }

πŸ”— See also ​

πŸ§‘β€πŸ’» Demo ​

Released under the MIT License