question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow initialization in the constructor

See original GitHub issue

For class, the following should be allowed, right? But I get a warning.

class Foo {
    readonly bar = 1; // OK
    readonly baz: string;
    constructor() {
        this.baz = "hello"; // OK
    }
}

tslint warning

[tslint] Modifying properties of existing object not allowed. (no-object-mutation)

tsconfig.json

{
    "extends": [
        "../../../../tslint.json"
    ],
    "rules": {
        "readonly-keyword": [true, "ignore-interface",  "ignore-class"],
        "readonly-array": true,
        "no-let": true,
        "no-this": true,
        "no-mixed-interface": true,
        "no-object-mutation": true
    }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jonaskellocommented, Dec 28, 2017

The no-object-mutation already has a ignore-prefix option. @veeramarni could you try to change your tslint.json to the one below and see if it helps?

{
    "extends": [
        "../../../../tslint.json"
    ],
    "rules": {
        "readonly-keyword": [true, "ignore-interface",  "ignore-class"],
        "readonly-array": true,
        "no-let": true,
        "no-this": true,
        "no-mixed-interface": true,
        "no-object-mutation": [true, {"ignore-prefix": ["this."]}]
    }
}
0reactions
ShanonJacksoncommented, Apr 15, 2018

I second this, iv’e just found you’re library recently and have been really happy with no longer having to spam readonly declarations everywhere to remind my team not to mutate.

however having to constantly spam tslint:disable:no-object-mutation in constructors is giving me cancer, please fix this sometime soon :<

ignore prefix [this.] is not a solution as i only want to enable mutation in constructor not anywhere else

Read more comments on GitHub >

github_iconTop Results From Across the Web

Explicit initialization with constructors (C++ only) - IBM
A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization ...
Read more >
Constructors and member initializer lists - cppreference.com
Constructor is a special non-static member function of a class that is used to initialize objects of its class type.
Read more >
Constructors for Initialization
A special member function of a class, called a constructor, is often introduced to initialize the values of some or all member variables...
Read more >
2.2. Creating and Initializing Objects: Constructors
Constructors initialize the attributes in newly created objects. · A constructor signature is the constructor name followed by the parameter list which is...
Read more >
A Guide to Java Initialization - Baeldung
Take a look at the different ways to initialize primitives and objects ... Let's discuss constructors and the new keyword in further detail....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found