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

docs: add explanation and suggested fix for string enforcement of MemoizedSelector type #1772

Merged
merged 6 commits into from
Apr 24, 2019
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
93 changes: 92 additions & 1 deletion projects/ngrx.io/content/guide/migration/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,97 @@ const getTodosById = createSelector(
);
```

#### MemoizedSelector enforces the return type to strictly match the second generic type.

For example, prior to 8.0.0 the following would be fine, since the return type `boolean` is widened to `boolean | null`.

BEFORE:

```ts
export const getLoginPagePending: MemoizedSelector<State, boolean | null> = createSelector(
selectLoginPageState,
loginState => loginState.pending // boolean
);
```

Now this will produce an error:
```
error TS2322: Type 'MemoizedSelector<State, boolean>' is not assignable to type 'MemoizedSelector<State, boolean | null>'.
Types of property 'setResult' are incompatible.
Type '(result?: boolean | undefined) => void' is not assignable to type '(result?: boolean | null | undefined) => void'.
Types of parameters 'result' and 'result' are incompatible.
Type 'boolean | null | undefined' is not assignable to type 'boolean | undefined'.
Type 'null' is not assignable to type 'boolean | undefined'.
```

Fix would be to specify the type correctly.

AFTER:

```ts
export const getLoginPagePending: MemoizedSelector<State, boolean> = createSelector(
selectLoginPageState,
loginState => loginState.pending // boolean
);
```

Another interesting case is when object literals are returned, e.g.

BEFORE:
```ts
interface Reaction {
happy: boolean;
tweet: string;
}
export const getNews: MemoizedSelector<State, Reaction> = createSelector(
newsState,
(news) => {
if (news.isFake) {
return {
happy: false,
tweet: 'blah blah blah',
}
}
return {
happy: true,
tweet: 'anyway',
}
}
);
```

Now the error message would happen (and it is a bit cryptic):
```
Type 'MemoizedSelector<State, { happy: false; tweet: string; } | { happy: true; tweet: string; }>' is not assignable to type 'MemoizedSelector<State, Reaction>'.
Type 'Reaction' is not assignable to type '{ happy: false; tweet: string; } | { happy: true; tweet: string; }'.
Type 'Reaction' is not assignable to type '{ happy: true; tweet: string; }'.
Types of property 'happy' are incompatible.
Type 'boolean' is not assignable to type 'true'.ts(2322)
```

Fix would be to add the return type to the `projector` function

AFTER:

```ts
export const getNews: MemoizedSelector<State, Reaction> = createSelector(
newsState,
(news): Reaction => {
if (news.isFake) {
return {
happy: false,
tweet: 'blah blah blah',
}
}
return {
happy: true,
tweet: 'anyway',
}
}
);
```


### @ngrx/router-store

#### Required usage of `forRoot`
Expand Down Expand Up @@ -148,4 +239,4 @@ To stay consistent with the other `@ngrx/*` packages, the following has been ren

The installation of `@ngrx/data` package via `ng add @ngrx/data` will remove `angular-ngrx-data` from the `package.json` and will also perform these renames.

</div>
</div>