【发布时间】:2022-02-18 06:49:00
【问题描述】:
我想从字典“数据”中删除所有出现的“__typename”。我使用了这个stackoverflow帖子中的一个函数:How to remove all instances of a specific key in an object?,它输出了正确的答案,但我的编译器显示this error message。我是 Typescript 的新手,我很难找到解决方案。
代码 sn-p:
import * as R from ramda;
// Dictionary "data"
const data = {
"subscription": {
"id": "10",
"subscribers": [
{
"id": "2017",
"username": "potato1",
"__typename": "UserType"
},
],
"unsubscribers": [
{
"id": "2022",
"username": "potato2",
"__typename": "UserType"
}
],
"__typename": "SubscriptionType"
},
"__typename": "Subscribe"
};
// Function from linked stackoverflow post, works but get a syntax error (see screenshot)
const removePropDeep = R.curry((prop, data) =>
R.when(
R.is(Object),
R.pipe(R.dissoc(prop), R.map(removePropDeep(prop))),
data
)
);
console.log(removePropDeep("__typename", data));
预期:
{
"subscription": {
"id": "10",
"subscribers": [
{
"id": "2017",
"username": "potato1",
},
],
"unsubscribers": [
{
"id": "2022",
"username": "potato2",
}
],
},
};
错误信息:
"Function implicitly has return type 'any'
because it does not have a return type annotation
and is referenced directly or indirectly
in one of its return expressions."
【问题讨论】:
-
请不要链接错误消息的图片。而是将相关输出复制并粘贴到您的问题中。见idownvotedbecau.se/imageofanexception
-
__typename是(例如)apollo 中需要的私有字段来理解架构,你不应该真的需要删除它恕我直言......但是,请查看stackoverflow.com/a/51380645/4099454
标签: typescript ramda.js