Searching for a method to convert a typescript interface into a JSON object?
I'm in need of a function that can take an interface as a parameter.
function transformInterfaceToResponse(input: any): DesiredInterface {
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(schema);
});
return response;
}
This is how I intend to use the function:
interface InterfaceX {
a: string;
b: boolean
}
const x = interfaceX converted to object;
const y = transformInterfaceToResponse(x);
Can you suggest a way for me to achieve this constant x?