带有特殊字符的打字稿字段 [英] Typescript fields with special characters

查看:104
本文介绍了带有特殊字符的打字稿字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要从Typescript中的node.js代码中调用REST API,并希望具有用于请求和响应的相应类型. 不幸的是,给定接口的JSON包含诸如以下字段:

We need to call a REST API from out node.js code in Typescript and would like to have corresponding types for requests and responses. Unfortunately, JSON for the interface we are given contains fields such as:

{...
    "@ID": "1234567890",
        ...
    "@Name": "something",
    "#text": "content",
...
}

我们被告知,这源于另一端的自动"JSONifier"的限制,无法更改.我们如何设置适合于此的接口? 以下显然不起作用:

We have been told, that this stems from a limitation of the other end's automated "JSONifier" and cannot be changed. How do we set up interfaces that fit for this? The following obviously won't work:

  export interface Request {
    @ID: string;
    @Name: string;
    #text: string;
    ...
  }

推荐答案

就像JS对象一样,您可以在名称两边加上引号,就像这样:

Just like JS objects, you can surround the names with quotes, like so:

  export interface Request {
    "@ID": string;
    "@Name": string;
    "#text": string;
  }

这篇关于带有特殊字符的打字稿字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆