有可能将TypeScript接口转换为JSON对象吗?
我有一个函数需要接收一个代表接口的参数。
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;
}
我希望能够像这样使用这个函数:
interface InterfaceX {
a: string;
b: boolean
}
const x = InterfaceX parsed as an object;
const y = parseIntoResponse(x);
有没有办法让我获得这个常量x?