- 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 ?
- 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);
- 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 ?
- 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 ifitem
doesn't havecounter
? you can't not have thecounter
member inItem
, it's either there or not. But you can have it's valuenull
orundefined
. 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
1 Answer
Reset to default 6It'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;
}
}