Skip to content
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

feat(store): add default generic type to Store and MockStore #2325

Merged
merged 2 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion modules/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { ReducerManager } from './reducer_manager';
import { StateObservable } from './state';

@Injectable()
export class Store<T> extends Observable<T> implements Observer<Action> {
export class Store<T = object> extends Observable<T>
implements Observer<Action> {
constructor(
state$: StateObservable,
private actionsObserver: ActionsSubject,
Expand Down
2 changes: 1 addition & 1 deletion modules/store/testing/src/mock_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (typeof afterEach === 'function') {
}

@Injectable()
export class MockStore<T> extends Store<T> {
export class MockStore<T = object> extends Store<T> {
static selectors = new Map<
| string
| MemoizedSelector<any, any>
Expand Down
20 changes: 20 additions & 0 deletions projects/ngrx.io/content/guide/store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ selectTotal.release();
*/
</code-example>

## 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`. 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.

<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:

<code-example header="app.component.ts">
export class AppComponent {
counter$ = this.store.select(fromCounter.selectCounter);

constructor(private readonly store: Store) {}
}
</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