Skip to content

Commit d472757

Browse files
alex-okrushkobrandonroberts
authored andcommitted
feat(entity): add undefined to Dictionary's index signature (#1719)
BREAKING CHANGE: Dictionary could be producing undefined but previous typings were not explicit about it.
1 parent 0abc948 commit d472757

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

modules/entity/spec/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ts_test_library(
99
),
1010
deps = [
1111
"//modules/entity",
12+
"//modules/store",
1213
"@npm//rxjs",
1314
],
1415
)
@@ -17,6 +18,5 @@ jasmine_node_test(
1718
name = "test",
1819
deps = [
1920
":test_lib",
20-
"//modules/entity",
2121
],
2222
)

modules/entity/spec/state_selectors.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
AnimalFarm,
77
TheGreatGatsby,
88
} from './fixtures/book';
9+
import { MemoizedSelector, createSelector } from '@ngrx/store';
910

1011
describe('Entity State Selectors', () => {
1112
describe('Composed Selectors', () => {
@@ -89,6 +90,14 @@ describe('Entity State Selectors', () => {
8990
expect(entities).toEqual(state.entities);
9091
});
9192

93+
it('should type single entity from Dictionary as entity type or undefined', () => {
94+
// MemoizedSelector acts like a type checker
95+
const singleEntity: MemoizedSelector<
96+
EntityState<BookModel>,
97+
BookModel | undefined
98+
> = createSelector(selectors.selectEntities, enitites => enitites[0]);
99+
});
100+
92101
it('should create a selector for selecting the list of models', () => {
93102
const models = selectors.selectAll(state);
94103

modules/entity/src/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export type IdSelectorNum<T> = (model: T) => number;
99
export type IdSelector<T> = IdSelectorStr<T> | IdSelectorNum<T>;
1010

1111
export interface DictionaryNum<T> {
12-
[id: number]: T;
12+
[id: number]: T | undefined;
1313
}
1414

1515
export abstract class Dictionary<T> implements DictionaryNum<T> {
16-
[id: string]: T;
16+
[id: string]: T | undefined;
1717
}
1818

1919
export interface UpdateStr<T> {

0 commit comments

Comments
 (0)