繁体   English   中英

将typescript接口转换成json object

[英]Convert typescript interface into json object

有没有办法将 typescript 接口转换为 json object?

我有一个 function 我想接收一个代表接口的参数。

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;
}

我想像这样使用这个 function :

interface InterfaceX {
  a: string;
  b: boolean
}

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

有没有办法让我得到这个 const x?

不,接口在运行时不存在,所以这是不可能的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM