0

I'm trying to figure out whether my object is an instance of interface Group or interface User. Here is my code

interface Group {
    id: string,
    name: string
}

interface User {
    id: string,
    name: string,
    age: number,
    contact:number
}

Now, when at some place when I try to filter out a list on the basis of instance type as

// rows is an array of mixed objects(Group & User), I want to filter out Group objects
rows=rows.filter((row:any)=> {
            return row instanceof Employee;  // Here it gives the error... 
          });

It gives the error as

'Employee' only refers to a type, but is being used as a value here.

Please, guide me how I can filter the list based on object type?

DevLoverUmar
  • 11,809
  • 11
  • 68
  • 98
  • 2
    please look at this answer https://stackoverflow.com/questions/46703364/why-does-instanceof-in-typescript-give-me-the-error-foo-only-refers-to-a-ty – Waqar Akram Oct 12 '20 at 11:01
  • Right-hand parameter in `object instanceof constructor` expression should be constructor/function. Types are erased at compile time, so you can't use interface in this position – Aleksey L. Oct 12 '20 at 11:01
  • The issue is that instanceof is a construct from JavaScript, and in JavaScript, instanceof expects a value for the right-side operand. Specifically, in row instanceof Employee JavaScript will perform a runtime check to see whether Employee.prototype exists anywhere in the prototype chain of row. – Waqar Akram Oct 12 '20 at 11:04

0 Answers0