Skip to content

docs(store): add note about using createFeatureSelector with no generic supplied to Store #3063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions projects/ngrx.io/content/guide/store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ selectTotal.release();

## Using Store Without Type Generic

When injecting `Store` into components and other injectables, it is possible to omit the generic type. If injected without the generic, the default generic is applied as follows `Store<T = object>`.
The most common way to select information from the store is to use a selector function defined with `createSelector`. TypeScript is able to automatically infer types from `createSelector`, which reduces the need to provide the shape of the state to `Store` via a generic argument.

The most common way to select information from the store is to use a selector function defined with `createSelector`. When doing so, TypeScript is able to automatically infer types from the selector function, therefore reducing the need to define the type in the store generic.
So, when injecting `Store` into components and other injectables, the generic type can be omitted. If injected without the generic, the default generic applied is `Store<T = object>`.
Comment on lines -251 to +253
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • removed use of passive voice
  • placed motivation to use Store without type generic at beginning of section


<div class="alert is-important">
It is important to continue to provide a Store type generic if you are using the string version of selectors as types cannot be inferred automatically in those instances.
</div>

The follow example demonstrates the use of Store without providing a generic:
The follow example demonstrates the use of `Store` without providing a generic:

<code-example header="app.component.ts">
export class AppComponent {
Expand All @@ -266,6 +266,29 @@ export class AppComponent {
}
</code-example>

When using strict mode, the `select` method will expect to be passed a selector whose base selects from an `object`.

This is the default behavior of `createFeatureSelector` when providing only one generic argument:

<code-example header="index.ts">
import { createSelector, createFeatureSelector } from '@ngrx/store';

export const featureKey = 'feature';

export interface FeatureState {
counter: number;
}

// selectFeature will have the type MemoizedSelector&lt;object, FeatureState&gt;
export const selectFeature = createFeatureSelector&lt;FeatureState&gt;(featureKey);

// selectFeatureCount will have the type MemoizedSelector&lt;object, number&gt;
export const selectFeatureCount = createSelector(
selectFeature,
state => state.counter
);
</code-example>

## Advanced Usage

Selectors empower you to compose a [read model for your application state](https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs#solution).
Expand Down