3

I have the following interface:

export interface IDateRangeOperation {
    getDateRange(): DateRange;
}

And have the following class:

export class DefaultRangeItem {
    name: String;
    operation: IDateRangeOperation;
    constructor(name: String, operation: IDateRangeOperation){
        this.name = name;
        this.operation = operation;

    }
    isEqual(defaultRangeItem: DefaultRangeItem): Boolean {
        return this.name === defaultRangeItem.name;
    }
    getDateRange(): DateRange {
        return this.operation.getDateRange();
    }
}

I have several classes that Implements the IDateRangeOperation

What I want is a way to compare, in the isEqual function, the operation object of the two DefaultRangesItems (on that receives on the isEqual function with the present on the current DefaultRangeItem)

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
  • I think we need to know how *you* define equivalence between `IDateRangeOperation` values before we can advise you. Are they equal if they are instances of the same class? Are they only equal if they are instances of the same class and have identical as-yet-unknown properties? Are they only equal if they are exactly the same objects in memory? Or is there some other notion of equality you have in mind? – jcalz Oct 23 '18 at 13:32
  • I just want to know if they are instances of the same class – Ricardo Rocha Oct 23 '18 at 15:12
  • Then is this just a duplicate of [this question](https://stackoverflow.com/questions/24959862/how-to-tell-if-two-javascript-instances-are-of-the-same-class-type)? – jcalz Oct 23 '18 at 15:23
  • @jcalz That worked for me. I will answer this question pointing how i solve the problem for this particularly case and will mark as duplicated – Ricardo Rocha Oct 24 '18 at 16:45

1 Answers1

0

As the @jcalz said in the comments, this is a duplicated question, but I will answer it by pointing what I change to make it work, based on the information given on this post. Remember:

Typescript is a typed superset of Javascript that compiles to plain Javascript.

So, if works in javascript, works in typescript.

Back to my case, I change this:

isEqual(defaultRangeItem: DefaultRangeItem): Boolean {
    return this.name === defaultRangeItem.name;
}

To this:

isEqual(defaultRangeItem: DefaultRangeItem): Boolean {
        return this.key === defaultRangeItem.key && this.name === defaultRangeItem.name &&
                this.operation.constructor === defaultRangeItem.operation.constructor;
    }

And know the isEqual function also validate the type of the object that the operation: IDateRangeOperation; is holding.

Important note: This only verifies if the objects operation are instance of the same class.

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130