marcos
marcos

Reputation: 95

TypeScript: How to set object property in constructor (depending on object properties)

  1. If item in Constructor has property counter (item.counter) i need to create this property in constructor element (this.counter). How to create this property only if this property has item ?
  2. And next question, how to create new property in constructor, by condition (e.g. if item.is_owner = true, i need to create this.owner = "Owner")

export class Item {
    public counter: number;
    public is_owner: boolean;
    public owner: string;
    
    
    constructor(item) {
        this.counter = item.counter; // if item has counter, i need create this.counter
        this.owner = "Owner"; // if item.is_owner == true ???
    }
}

var element = new  Item (item);

Upvotes: 3

Views: 12089

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164137

It's hard to understand what you're trying to do from your code, for example what's this item that the ctor gets as a parameter? is it another instance of Item or another type?
Also, the whole thing with the owner isn't clear.

In any case, your class either has a defined property or doesn't.
You can of course add more properties in the ctor without defining them as members but that will cause typescript compilation errors which you probably prefer to avoid, for example:

class Point {
    public x: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y; // error: Property `y` does not exists on type `Point`
    }
}

You can solve that with casting to any:

class Point {
    public x: number;

    constructor(x: number, y: number) {
        this.x = x;
        (this as any).y = y; // no error
    }
}

But then this is a problem:

let p = new Point(10, 5);
console.log(p.x);
console.log(p.y); // error: Property `y` does not exists on type `Point`

Here to you can use any: console.log((p as any).y); but then you bypass the compiler type checking and if you're doing that then why bother with typescript at all?

What you can do if you want to avoid having the member with null or undefined is to have different implementations of the same interface/base class and use a factory function to create the right implementation based on the received data, something like:

interface ItemData {
    counter?: number;
}

class BaseItem {
    static create(data: ItemData): BaseItem {
        if (data.counter) {
            return new ItemWithCounter(data);
        }

        return new BaseItem(data);
    }

    constructor(data: ItemData) {}
}

class ItemWithCounter extends BaseItem {
    private counter: number;

    constructor(data: ItemData) {
        super(data);
        this.counter = data.counter;
    }
}

Upvotes: 6

Related Questions