具有两个类型参数的ngrx createFeatureSelector

时间:2018-07-19 22:51:56

标签: angular ngrx

我正在学习ngrx,目前将v6.0.1与Angular v6.0.9和AngularCLI v6.0.8一起使用。

示例应用程序显示了如何使用两个这样的类型参数创建根级别的createFeatureSelector:

example-app/app/reducers/index.ts

.....

export const getLayoutState = createFeatureSelector<State, fromLayout.State>(
  'layout'
);

当我尝试在我的应用中重现此示例时,编译器抱怨createFeatureSelector需要1个参数,但我提供了2个。

我一直在调查中,发现在我的应用程序中通过在导入的createFeatureSelector函数上按F12在vscode中导航时,将我带到此处显示的行:

selector.d.ts
.....

export declare function createSelectorFactory<T = any, V = any>(memoize: MemoizeFn): (...input: any[]) => Selector<T, V>;
export declare function createSelectorFactory<T = any, V = any>(memoize: MemoizeFn, options: SelectorFactoryConfig<T, V>): (...input: any[]) => Selector<T, V>;
export declare function createFeatureSelector<T>(featureName: string): MemoizedSelector<object, T>;
/\/\/\

selector.d.ts文件中没有导出的功能。

但是,当我下载ngrx平台存储库并在同一行上导航时,我最终到达了这一行:

selector.ts
.....

export function createFeatureSelector<T>(
  featureName: string
): MemoizedSelector<object, T>;
export function createFeatureSelector<T, V>(
  featureName: keyof T
): MemoizedSelector<T, V>;
/\/\/\/\/\
export function createFeatureSelector(
  featureName: any
): MemoizedSelector<any, any> {
  return createSelector(
    (state: any) => state[featureName],
    (featureState: any) => featureState
  );
}

这看起来更准确,因为签名包含两个类型参数。

当我将@ ngrx / store的错误版本添加到package.json时,我在哪里做错了吗?这是一个示例:

....
"moment": "2.22.2",
"npm": "^6.1.0",
"@ngrx/effects": "6.0.1",
"@ngrx/router-store": "6.0.1",
"@ngrx/store": "6.0.1",
"@ngrx/store-devtools": "6.0.1",
"ngrx-store-freeze": "0.2.4",
"primeng": "^5.2.7",
"rxjs": "6.2.1",
....

3 个答案:

答案 0 :(得分:2)

这是一个未发布的功能,将在下一版本中提供。
您现在可以使用下面的代码。

export const getLayoutState = createFeatureSelector<fromLayout.State>('layout');

答案 1 :(得分:0)

您只需要为要创建选择器的状态传递接口,如下所示:

export interface LayoutState {
  layout: fromLayout.State;
  somethingElse: fromsomethingElse.State;
  ...: .....;
}

export const getLayoutState = createFeatureSelector<LayoutState>('layout')

布局状态只是一个用于更好地键入和帮助IDE的界面。

答案 2 :(得分:0)

  • EDIT-由ngrx开发人员Tim接受的答案解决。

我已为我的问题在ngrx存储库上创建了一个错误,并将在此处进行更新。

https://github.com/ngrx/platform/issues/1191