From 8c1badba8edacba12e720047b33a1f149bcf3457 Mon Sep 17 00:00:00 2001 From: aokrushko Date: Tue, 9 Apr 2019 09:26:02 -0400 Subject: [PATCH] feat(entity): add undefined to Dictionary's index signature --- modules/entity/spec/BUILD | 2 +- modules/entity/spec/state_selectors.spec.ts | 9 +++++++++ modules/entity/src/models.ts | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/modules/entity/spec/BUILD b/modules/entity/spec/BUILD index 299a089798..54a5b0b50b 100644 --- a/modules/entity/spec/BUILD +++ b/modules/entity/spec/BUILD @@ -9,6 +9,7 @@ ts_test_library( ), deps = [ "//modules/entity", + "//modules/store", "@npm//rxjs", ], ) @@ -17,6 +18,5 @@ jasmine_node_test( name = "test", deps = [ ":test_lib", - "//modules/entity", ], ) diff --git a/modules/entity/spec/state_selectors.spec.ts b/modules/entity/spec/state_selectors.spec.ts index 7a2c47db41..b298674143 100644 --- a/modules/entity/spec/state_selectors.spec.ts +++ b/modules/entity/spec/state_selectors.spec.ts @@ -6,6 +6,7 @@ import { AnimalFarm, TheGreatGatsby, } from './fixtures/book'; +import { MemoizedSelector, createSelector } from '@ngrx/store'; describe('Entity State Selectors', () => { describe('Composed Selectors', () => { @@ -89,6 +90,14 @@ describe('Entity State Selectors', () => { expect(entities).toEqual(state.entities); }); + it('should type single entity from Dictonary as entity type or undefined', () => { + // MemoizedSelector acts like a type checker + const singleEnitity: MemoizedSelector< + EntityState, + BookModel | undefined + > = createSelector(selectors.selectEntities, enitites => enitites[0]); + }); + it('should create a selector for selecting the list of models', () => { const models = selectors.selectAll(state); diff --git a/modules/entity/src/models.ts b/modules/entity/src/models.ts index 8a81c1bf5b..9ab192851e 100644 --- a/modules/entity/src/models.ts +++ b/modules/entity/src/models.ts @@ -9,11 +9,11 @@ export type IdSelectorNum = (model: T) => number; export type IdSelector = IdSelectorStr | IdSelectorNum; export interface DictionaryNum { - [id: number]: T; + [id: number]: T | undefined; } export abstract class Dictionary implements DictionaryNum { - [id: string]: T; + [id: string]: T | undefined; } export interface UpdateStr {