JS / TS determine if a JSON object implements a class or interface JS / TS determine if a JSON object implements a class or interface json json

JS / TS determine if a JSON object implements a class or interface


Use typeof and function overloads to get better type hints:

// narrow down types to get better typingsfunction parse(a: string, b: {}, c: string): Parser1;//...// implement genericallyfunction parse(a: any, b: any, c: any): Parser { if(typeof a === "string" && typeof b === "object" && typeof c === "string") {   return Parser1.parse(a, b, c); } /*...*/}


Use the following to check each

let obj = {};let str = '';let num = 0;let array = [];console.log(Array.isArray(array))console.log(typeof obj === 'object')console.log(typeof str === 'string')console.log(!isNaN(num))