'How to check type of object in typescript

I have a JSON file containing two different types of Objects in it. I want to iterate over the JSON with respect to their type. To illustrate my problem I created two different type of Object Animal and Person

Now I want to check the type of Object whether it is an Animal or Person.

type Animal = {
  name: string;
  legs: number;
}

type Person = {
  name: number;

}

const checkType = (isAnimalOrPerson: Animal | Person) =>{
  if(isAnimalOrPerson instanceof Animal){
     console.log("animal type");
  }
  else{

  }
}

Here I am getting an error on if the condition says: "Animal" only refers to a type, but is being used as a value here.



Solution 1:[1]

The "problem" with type is that it's a concept that only exists during development; once you transpile, it disappears, and TypeScript can't foresee the shape of a runtime value you might pass (unless you do assertion, i.e. explicit checks about what properties an object has). If you want to do type checking, you have to do it in another way, for example using classes, and then the approach would only work if you actually instantiate the class (as opposed to just using it as a shape):

class Animal {
    name: string;
    age: number;
    legs: number;
}

class Person {
    name: string;
    age: number;
    job: string;
    education: string;
}

let a: Animal = new Animal();
let b: Animal = { name: "Rex", legs: 4, age: 3 };
let c = new Person();

function checkType(arg: Animal | Person): string {
    if (arg instanceof Animal) {
        return "This is an animal";
    } else if (arg instanceof Person) {
        return "This is a person";
    }

    return "Can't tell";
}

console.log(checkType(a)); // This is an animal
console.log(checkType(b)); // Can't tell
console.log(checkType(c)); // This is a person

Solution 2:[2]

There's varoius ways to check PersonorAnimal value is also satisfies Animal.

  1. coerce by property
if ('legs' in toBeDetermined) {
  // toBeDetermined should be `Animal` in this block
}
  1. give Animal a unique symbol and check them
const animalTag: unique symbol = Symbol("Animal tag");

type Animal = {
  __brand__: typeof animalTag;
  name: string;
  legs: number;
}

if (toBeDetermined.__brand__ === animalTag) {
  // toBeDetermined can be coerced manually into Animal
}

I prefer 1 because Animal has disjoint property legs.

Solution 3:[3]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamen Minkov
Solution 2 user-id-14900042
Solution 3 Rungkrit Titachotnimit