最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript: How to set object property in constructor (depending on object properties) - Stack Overflow

programmeradmin3浏览0评论
  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);
  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);

Share Improve this question edited Aug 7, 2016 at 10:19 marcos asked Aug 7, 2016 at 9:53 marcosmarcos 951 gold badge2 silver badges7 bronze badges 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? – Nitzan Tomer 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. – marcos Commented Aug 7, 2016 at 9:58
  • " what happens if item doesn't have counter?" element.counter will not have this property as well :) – marcos Commented Aug 7, 2016 at 10:06
Add a ment  | 

1 Answer 1

Reset to default 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 pilation 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 piler 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;
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论