Reputation: 95
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
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