Is there a way to check if something is an instance of an object without importing
that object?
The class that I am using instanceof
is a child of the current class. I have a method that runs a function and returns a new instance of either Model
or Row
based on some if statements. Model
extends the class that has the method that I am using instanceof
in, and I want to check if it is an instance of Model
and then do some additional operations.
export class Base {
public firstOrFail() {
let val = this.first() // Returns a new instance of Model or Row
if(val instanceof Model && val.length == 0) {
throw new Error('No values')
}
}
}
Then I have two files each looking like this:
// File Model.ts
import { Base } from './Base'
export class Model extends Base {}
// File Row.ts
export class Row {}
The issue I am having is if I import Model
inside base I get a circular dependency loop. If I don't require it, I get an error:
Cannot find name 'Model'
So is there a way I can do instanceof
within the Base
class?
There are two ways this can be used:
new Base().firstOrFail()
or
new Model().firstOrFail()