Skip to content

Commit

Permalink
feat(store): add default generic type to Store and MockStore (#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleygrimes authored Feb 5, 2020
1 parent ba37ad8 commit 09daeb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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

0 comments on commit 09daeb9

Please sign in to comment.