[Solved] How to access a constructor parameter inside a Typescript method


“Just because my method cannot access a constructor parameter…” is thinking about this in the wrong terms. When instantiating your class, is productDto an important state of your instance? Does the data in productDto hold important information that Product needs during its lifetime? Then you’re not merely trying to pass it between your constructor and some method, but that data is part of your object’s state. And your object methods use and depend on that state. And object state is always implemented by object properties, so saving productDto on this.productDto is perfect fine and the right way to do it.

But this doesn’t change the fact that the property is still accessible once the object has been initialized.

So what? Using TypeScript and the private declaration, TypeScript will ensure that you’re not shooting your own foot by trying to access that property from outside code. That’s all you need. There’s no point in actually trying to enforce non-accessibility. You’re just going to bend over backwards for hardly any gain. Many Javascript objects have internal state which you can poke around in if you so wish. That’s no problem at all.

It’s only a problem if you’re actually writing code against these internal properties which aren’t supposed to be part of the official API, and that internal implementation changes down the line. Simply don’t do that. Again, TypeScript’s private helps you enforce exactly that.

this.id = productDto.id;
this.productDto = productDto;

This is pretty redundant. Quite apparently you need productDto.id and productDto later on in other methods. Simply store the entire productDto as a property and access its .id as needed. If you need to expose the id as property on your class, using a get method which returns this.prodctDto.id is probably a sensible idea:

class Product {
    private productDto: IProductDto;

    constructor(productDto: IProductDto) {
        this.productDto = productDto;
    }

    get id(): string {
        return this.productDto.id;
    }

    getProductAttributeByName(name: string): ProductAttribute {
        return this.productDto.productAttributes.filter(x => x.name === name)
    }
}

In general, try designing your classes without writing the constructor first. Write everything but the constructor. Design which internal and external properties your class needs and which properties your methods will get their data from. Then, as the very last step, write your constructor which needs to ensure that the object state is such that all your methods can work as designed. Then the thinking of “passing from constructor to method” obviates itself.

6

solved How to access a constructor parameter inside a Typescript method