Redux-Observable-打字稿错误:“类型'{}'中缺少属性'type',但类型中必需”

时间:2019-10-22 20:57:42

标签: reactjs typescript redux rxjs redux-observable

我正在使用redux-observable,并且遇到无法理解的Typescript错误,并且如果我在rxjs管道中有太多的代码行,它似乎是随机出现的:

df = pd.DataFrame(columns=['data'], data=['what are you doing', 'give me the the file', 'the sun comes up up', 'you and me'])
word_list = ['the', 'up', 'me']
df['words'] = df['data'].str.split().apply(lambda i: list(set(i))) # making sure a word occurs only once per row
all_words = [i for j in df['words'].values.tolist() for i in j]
d = {}
for i in word_list:
    d[i] = all_words.count(i)

d
{'the': 2, 'up': 1, 'me': 2}

这是有错误的代码。我在管道中添加了许多Type 'Observable<{}>' is not assignable to type 'Observable<{ type: "feed/PUSH"; payload: Profile; } | { type: "feed/POP"; } | { type: "feed/SWIPE"; payload: Swipe; } | { type: "feed/FETCH_NEXT_IDS"; } | { type: "feed/PUSH_NEXT_IDS"; payload: { nextIds: string[]; nextPage: number; }; } | { ...; } | { ...; } | { ...; }>'. Type '{}' is not assignable to type '{ type: "feed/PUSH"; payload: Profile; } | { type: "feed/POP"; } | { type: "feed/SWIPE"; payload: Swipe; } | { type: "feed/FETCH_NEXT_IDS"; } | { type: "feed/PUSH_NEXT_IDS"; payload: { nextIds: string[]; nextPage: number; }; } | { ...; } | { ...; } | { ...; }'. Property 'type' is missing in type '{}' but required in type '{ type: "feed/FETCH_NEXT_PROFILE"; }'.ts(2322) action.d.ts(36, 5): 'type' is declared here. index.d.ts(36, 26): The expected type comes from the return type of this signature. 函数,以表明仅当我有太多tap时才会出现错误。如果删除定义的数量tap,该错误似乎消失了。

tap

1 个答案:

答案 0 :(得分:1)

我怀疑您看到的内容与RxJS的pipe()实用工具仅对pass in 9 operations提供类型安全支持这一事实有关。

一旦超过该数字,它就会退回给类型安全性较低的签名,这可能会导致您的问题。无论是在RxJS,Redux Observable的某种类型签名中揭示实际的错误,还是代码中的错误,或者它只是“实际上必须达到的状态”,或者是其他原因,我不能立即确定- -抱歉!只是想了解一下,至少会让您知道这很有可能是相关的。

您可以通过将操作分为多个子流来避免这种情况,即不要用管道输送尽可能多的运算符。当然,不一定要推荐它是很好的做法,无论如何这样做总是有帮助的,以便为您的代码提供喘息的空间,并为以后维护此代码的其他开发人员提供上下文。


RxJS中这样做的原因是,尽管有discussions around this for many many years,但TypeScript(在撰写本文时)不支持可变参数泛型或可变参数类型。