javascripttypescript

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

  • 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