@ngrx/reducer:createReducer() 和 on() 不是类型安全的吗?

M. *_*Lee 8 ngrx angular

在使用ngrx我未使用createAction()and 的以前版本时, createReducer()如果我尝试添加任何不属于所提供的State.

我将应用程序升级到Angular 8,并且ngrx 8.3.0也在使用。现在似乎类型安全不再存在,尽管NGRX文档指出大部分类型推断将被实现。

someState.ts

export interface ISampleState {
  id: number
}
Run Code Online (Sandbox Code Playgroud)

someAction.ts

export const request = createAction('[SAMPLE] Request', props<{id: number}>();
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

在实际运行代码后,我会看到DevToolsstore 确实填充了这个无根据的属性。

我最大的担忧是当我在return块内的减速器中有一个错字时,它也不会抱怨。

即如果我不小心打字

return {
   ...state,
   Id: id  //should be `id`
}
Run Code Online (Sandbox Code Playgroud)

很难找到错误,因为它编译得很好,没有抱怨。

有任何想法吗?

ber*_*wyn 7

玩了一会儿,这似乎是 TypeScript 的问题!createReducer最终创建一个ActionReducer<S, A>具有(state: S | undefined, action: A) => S.

在这种情况下,TypeScript 似乎允许任何满足此签名的可调用对象满足参数,即使返回的类型具有更多的属性S。我已经向编译器提交了这个错误,因为对我来说这似乎不是正确的行为。