Tslint: Rule suggestion: prefer-parameter-properties

Created on 25 Apr 2017  ยท  9Comments  ยท  Source: palantir/tslint

There's a no-parameter-properties rule, but what if you like them? How about a rule that flags code like this:

class Foo {
  foo: number;
  constructor(foo: number) {
    this.foo = foo;
  }
}

in favor of this:

class Foo {
  constructor(public foo: number) {
  }
}

Another option would be to replace no-parameter-properties with a more general parameter-properties rule that lets you say how you feel about them.

See parameter properties in the TS handbook.

P2 Accepting PRs Breaking Change Rule Suggestion ๐ŸŒน R.I.P. ๐ŸŒน

Most helpful comment

Sure, that sounds good. I would like it to be one rule though, so let's add parameter-properties with options "always" | "never" and deprecate no-parameter-properties.

All 9 comments

Sure, that sounds good. I would like it to be one rule though, so let's add parameter-properties with options "always" | "never" and deprecate no-parameter-properties.

What was the motivation for this rule in the first place, BTW? I only see benefits to the "always" way, so I'm curious. :)

@codingthat mostly just consistency with other languages like Java for full-stack developers.

@adidahiya That being the case, I'm even happier to see the rule go neutral. :)

I mean, it _is_ harder to see the list of class members at a glance when you use parameter properties. New language features almost always add more cognitive load.

Depends on your formatting, maybe :) That's a good point to consider though.

FWIW, parameter properties are quite nice when using libraries such as Inversify:

@injectable()
class Ninja implements Warrior {

    private _katana: Weapon;
    private _shuriken: ThrowableWeapon;

    public constructor(
        @inject(TYPES.Weapon) katana: Weapon,
        @inject(TYPES.ThrowableWeapon) shuriken: ThrowableWeapon
    ) {
        this._katana = katana;
        this._shuriken = shuriken;
    }

    public fight() { return this._katana.hit(); };
    public sneak() { return this._shuriken.throw(); };
}

..compared to...

@injectable()
class Ninja implements Warrior {
    public constructor(
        @inject(TYPES.Weapon)
        private readonly katana: Weapon,

        @inject(TYPES.ThrowableWeapon)
        private readonly shuriken: ThrowableWeapon,
    ) {  }

    public fight() { return this.katana.hit(); };
    public sneak() { return this.shuriken.throw(); };
}

Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you _really_ need the rule, custom rules are always an option and can be maintained outside this repo!

๐Ÿ’€ _It's time!_ ๐Ÿ’€

TSLint is being deprecated and no longer accepting pull requests for new rules. See #4534. ๐Ÿ˜ฑ

If you'd like to see this rule implemented, you have two choices:

๐Ÿ‘‹ It was a pleasure open sourcing with you!

_If you believe this message was posted here in error, please comment so we can re-open the issue!_

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cateyes99 picture cateyes99  ยท  3Comments

ghost picture ghost  ยท  3Comments

rajinder-yadav picture rajinder-yadav  ยท  3Comments

denkomanceski picture denkomanceski  ยท  3Comments

jacob-robertson picture jacob-robertson  ยท  3Comments