Convert typescript interface into json object

Viewed 679

Is there a possible way to convert a typescript interface into a json object?

I have a function that I would like to receive a param that represents an interface.

function parseIntoResponse(input: any): MyNewInterface {
  const response = {
    description: 'lorem ipsum',
    schema: [],
  };

  const keys = Object.keys(input);
    
  keys.forEach(key => {
    const schema = {
      name: key,
      type: JSON.stringify(input[key]),
    };
    
    response.schema.push();
  });

  return response;
}

And I would like to use this function like this:

interface InterfaceX {
  a: string;
  b: boolean
}

const x = InterfaceX parsed as an object;
const y = parseIntoResponse(x);

Is there a way for me to get this const x?

1 Answers

No, interfaces don't exist at runtime, so this is not possible.

Related