Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-4473

Parameter properties should be used consistently

    XMLWordPrintable

    Details

    • Message:
      Hide
      * Usage of parameter properties has been disallowed.
      or
      * Use parameter properties instead of assigning to members in the constructor body.
      Show
      * Usage of parameter properties has been disallowed. or * Use parameter properties instead of assigning to members in the constructor body.
    • Highlighting:
      Hide
      • parameter property declaration
        or
      • assignment statement
      Show
      parameter property declaration or assignment statement
    • List of parameters:
      Hide

      key: forceParameterProperties
      description: Forces to use parameter properties when true, forbids their use when false
      default: true

      Show
      key: forceParameterProperties description: Forces to use parameter properties when true, forbids their use when false default: true
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      TypeScript
    • Irrelevant for Languages:
      ABAP, C#, C, C++, Cobol, CSS, Flex, HTML, Java, JavaScript, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Swift, T-SQL, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources
    • TSLint:
      parameter-properties, no-parameter-properties

      Description

      Parameter properties let you both create and initialize a member in one place, and omit an explicit member declaration and the assignment of the constructor parameter to the member. To use a parameter property, add an accessibility modifier or readonly, or both in front of the constructor parameter.

      constructor(readonly name: string, private age: number) { // creates 2 initialized members "name" and "age"
      }
      

      While this syntax is very concise it might be confusing for developers who are new to TypeScript.
      Shared conventions allow teams to collaborate efficiently. This rule checks that either parameter properties are used everywhere or not at all.

      Noncompliant Code Example

      Using the default parameter forceParameterProperties value true:

      class Person {
        name: number;
        constructor(name: string) {
          this.name = name; // Noncompliant, parameter property can be used
        }
      }
      

      Compliant Solution

      class Person {
        constructor(public name: string) {
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              elena.vilchik Elena Vilchik
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: