0

I have this type defined

export interface Hostel {
    id: String;
}

I want to check if an object is from that type, but there is no way. I have tried with

console.log ('************ ', (typeof result === 'Hostel'));
        console.log ('************ ', (typeof result === Hostel));
        console.log ('************ ', result instanceof Hostel);

I have this error:

'Hostel' only refers to a type, but is being used as a value here.
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80

1 Answers1

0

Types (which includes interfaces) are only available during development and compile time and not during runtime. You need to use a class if you want to check the type during runtime.

export class Hostel {
    constructor(public id: String){};
}

const result = new Hostel("foo");

console.log(result instanceof Hostel) // this will return true
mekwall
  • 28,614
  • 6
  • 75
  • 77