@ngrx/Reducer:createReducer()和on()不是类型安全的吗? [英] @ngrx/reducer: createReducer(), and on() are not being type-safe?
本文介绍了@ngrx/Reducer:createReducer()和on()不是类型安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ngrx
的以前版本时,我没有使用
createAction()
和createReducer()
,如果我尝试添加不属于所提供的State
的任何其他属性,则将引发错误。
我将应用程序升级到Angular 8
,并使用ngrx 8.3.0
。现在,尽管NGRX
文档指出将实现大多数类型推断,但类型安全似乎已不复存在。
someState.ts
export interface ISampleState {
id: number
}
someAction.ts
export const request = createAction('[SAMPLE] Request', props<{id: number}>();
someReducer.ts
const initialState: ISampleState = {
id: null
}
const sampleReducer = createReducer(
initialState,
on(SampleActions.request, (state, { id }) => {
return {
...state,
id,
// Below would previously complain since only `id` is defined in the `ISampleState`
thisShouldNormallyComplain: 'but it does not'
}
}
);
// AOT purposes
export function reducer(state: ISampleState | undefined, action: Action): ISampleState {
return sampleReducer(state, action);
}
在实际运行代码后,我会通过DevTools
看到存储确实填充了这个不必要的属性。
我最担心的是,当我在return
区块内的减速器中出现打字错误时,它也不会抱怨。
即如果我不小心键入
return {
...state,
Id: id //should be `id`
}
很难找到错误,因为它编译得很好,没有任何问题。
有什么想法吗?
推荐答案
试了一下,似乎这是TypeScrip的问题!createReducer
最终会创建一个签名为(state: S | undefined, action: A) => S
的ActionReducer<S, A>
。
S
更多的属性。我已对编译器提起this bug诉讼,因为在我看来这不是正确的行为。
这篇关于@ngrx/Reducer:createReducer()和on()不是类型安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文