0

In js/typescript, if I have an interface like this

interface B {
   bar:string;
}

interface A {
   foo:string;
   bar:B;
}

and then I have a abstract JS object like this

const r = await fetch("https://mylink.com");
const d = await r.json();

Is there a way to do something like

const matches:boolean = Matches(d, A);

which then is a boolean telling me if it is an exact match or not?

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474
  • 2
    You should be using a TypeScript-first validation library at this point, like [`zod`](https://www.npmjs.com/package/zod). – kelsny Mar 08 '23 at 16:59
  • Is there a way I can convert an existing typescript interface into a zod schema? – omega Mar 08 '23 at 17:20
  • yes https://www.google.com/search?q=interface+to+zod+schema&oq=interface+to+zod+schema&aqs=chrome..69i57j0i22i30.4680j0j4&sourceid=chrome&ie=UTF-8 – Alex Wayne Mar 08 '23 at 17:36
  • I meant dynamically in the code in runtime. – omega Mar 08 '23 at 17:40

0 Answers0