简体   繁体   English

如何将json中的数字转换为typescript界面​​中的字符串

[英]How to convert number in json to string in typescript interface

{
   "id": 13,
   "name": "horst",
}

to

 interface A {
    id: string;
    name: string;
}

when A json converts to A object, I expected to convert id from number to string automatically.A json 转换为A对象时,我希望自动将id从数字转换为字符串。 but it doesn't convert.但它不会转换。

i want to convert from number to string by type infer.我想通过类型推断从数字转换为字符串。 when a json parse to object.当 json 解析为对象时。 how can i do it?我该怎么做?

ps.附: sorry my English is poor..对不起我的英语很差..

Typescript doesn't include any type information at runtime. Typescript 在运行时不包含任何类型信息。 You need to use something like io-ts to perform runtime coercion of parsed types into typed data structures.您需要使用类似io-ts 的东西来将解析类型的运行时强制转换为类型化数据结构。

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

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