3
  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);

3
  • I'm not sure what you mean by "create this property only if this property has item", what happens if item doesn't have counter? you can't not have the counter member in Item, it's either there or not. But you can have it's value null or undefined. Is that what you're looking for? Commented Aug 7, 2016 at 9:56
  • Ok, thanks. Yes, of course, i can write this.counter = item.counter || null, but i thought that there is any way to create this.counter only if item.counter exist. Commented Aug 7, 2016 at 9:58
  • " what happens if item doesn't have counter?" element.counter will not have this property as well :) Commented Aug 7, 2016 at 10:06

1 Answer 1

6

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;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, thank you. "Also, the whole thing with the owner isn't clear." I just need create one more element type based on other type. Therefore, if item.is_owner == true, element.owner should look like element.owner == "Owner"
That's easy: this.owner = item.is_owner ? "Owner" : "NotOwner";
i do not need element.owner property if there is no item.is_owner or item.is_owner == false; I just want to avoid additional fields like: element.owner == 'null" or 'undefined' or ' '
So use the example I gave you in my example where there are different implementations which share the same interface/class. Again, you can either have a member or not, but it's not something you decide at runtime, it's static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.