From 682f66e77df6a5bc70c284b1bc491cddb2e47eb9 Mon Sep 17 00:00:00 2001 From: Denis Bendrikov Date: Wed, 1 Feb 2023 00:12:02 +0200 Subject: [PATCH] fix(akita): support TS strict mode This is a quick spike created to find out a way to fix akita for Angular 15+ in strict mode Refs: salesforce/akita#870 --- packages/akita/src/lib/entityStore.ts | 2 +- .../plugins/dirtyCheck/dirtyCheckPlugin.ts | 3 ++- packages/akita/src/lib/plugins/plugin.ts | 5 +++-- .../stateHistory/stateHistoryPlugin.ts | 3 ++- packages/akita/src/lib/queryEntity.ts | 2 +- packages/akita/src/lib/runStoreAction.ts | 20 +++++++++---------- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/akita/src/lib/entityStore.ts b/packages/akita/src/lib/entityStore.ts index 4ebc626e..fbd4f715 100644 --- a/packages/akita/src/lib/entityStore.ts +++ b/packages/akita/src/lib/entityStore.ts @@ -663,7 +663,7 @@ export class EntityStore extends EntityStore { +export class EntityUIStore extends EntityStore { _akitaCreateEntityFn: EntityUICreateFn; constructor(initialState = {}, storeConfig: Partial = {}) { diff --git a/packages/akita/src/lib/plugins/dirtyCheck/dirtyCheckPlugin.ts b/packages/akita/src/lib/plugins/dirtyCheck/dirtyCheckPlugin.ts index eced8116..d3a23404 100644 --- a/packages/akita/src/lib/plugins/dirtyCheck/dirtyCheckPlugin.ts +++ b/packages/akita/src/lib/plugins/dirtyCheck/dirtyCheckPlugin.ts @@ -6,6 +6,7 @@ import { isUndefined } from '../../isUndefined'; import { Query } from '../../query'; import { QueryEntity } from '../../queryEntity'; import { AkitaPlugin, Queries } from '../plugin'; +import { EntityState } from '../../types'; type Head = State | Partial; @@ -29,7 +30,7 @@ export type DirtyCheckResetParams = { updateFn?: StoreState | ((head: StoreState, current: StoreState) => any); }; -export class DirtyCheckPlugin extends AkitaPlugin { +export class DirtyCheckPlugin extends AkitaPlugin { private head: Head; private dirty = new BehaviorSubject(false); private subscription: Subscription; diff --git a/packages/akita/src/lib/plugins/plugin.ts b/packages/akita/src/lib/plugins/plugin.ts index 41e24d09..1658f9c5 100644 --- a/packages/akita/src/lib/plugins/plugin.ts +++ b/packages/akita/src/lib/plugins/plugin.ts @@ -6,10 +6,11 @@ import { toBoolean } from '../toBoolean'; import { getAkitaConfig } from '../config'; import { getValue } from '../getValueByString'; import { setValue } from '../setValueByString'; +import { EntityState } from '../types'; -export type Queries = Query | QueryEntity; +export type Queries = Query | QueryEntity; -export abstract class AkitaPlugin { +export abstract class AkitaPlugin { protected constructor(protected query: Queries, config?: { resetFn?: Function }) { if (config && config.resetFn) { if (getAkitaConfig().resettable) { diff --git a/packages/akita/src/lib/plugins/stateHistory/stateHistoryPlugin.ts b/packages/akita/src/lib/plugins/stateHistory/stateHistoryPlugin.ts index 5dca94fb..39b7197c 100644 --- a/packages/akita/src/lib/plugins/stateHistory/stateHistoryPlugin.ts +++ b/packages/akita/src/lib/plugins/stateHistory/stateHistoryPlugin.ts @@ -2,6 +2,7 @@ import { BehaviorSubject, distinctUntilChanged, Observable, pairwise } from 'rxj import { logAction } from '../../actions'; import { isFunction } from '../../isFunction'; import { AkitaPlugin, Queries } from '../plugin'; +import { EntityState } from '../../types'; export interface StateHistoryParams { maxAge?: number; @@ -15,7 +16,7 @@ export type History = { future: State[]; }; -export class StateHistoryPlugin extends AkitaPlugin { +export class StateHistoryPlugin extends AkitaPlugin { /** Allow skipping an update from outside */ private skip = false; diff --git a/packages/akita/src/lib/queryEntity.ts b/packages/akita/src/lib/queryEntity.ts index 82899cfb..b7f5558c 100644 --- a/packages/akita/src/lib/queryEntity.ts +++ b/packages/akita/src/lib/queryEntity.ts @@ -397,7 +397,7 @@ export class QueryEntity, I } // @internal -export class EntityUIQuery extends QueryEntity { +export class EntityUIQuery extends QueryEntity { constructor(store) { super(store); } diff --git a/packages/akita/src/lib/runStoreAction.ts b/packages/akita/src/lib/runStoreAction.ts index 7dc7151b..b41889dc 100644 --- a/packages/akita/src/lib/runStoreAction.ts +++ b/packages/akita/src/lib/runStoreAction.ts @@ -4,7 +4,7 @@ import { isNil } from './isNil'; import { Store } from './store'; import { configKey } from './storeConfig'; import { __stores__ } from './stores'; -import { Constructor } from './types'; +import { Constructor, EntityState } from './types'; export enum StoreAction { Update = 'UPDATE', @@ -60,7 +60,7 @@ export function getStoreByName, S = TStore extends Store * Get a {@link EntityStore} from the global store registry. * @param storeClass The {@link EntityStore} class of the instance to be returned. */ -export function getEntityStore, S = TEntityStore extends EntityStore ? T : never>(storeClass: Constructor): TEntityStore { +export function getEntityStore, S extends EntityState = TEntityStore extends EntityStore ? T : never>(storeClass: Constructor): TEntityStore { return getStore(storeClass as Constructor>) as TEntityStore; } @@ -68,7 +68,7 @@ export function getEntityStore, S = TEntityS * Get a {@link EntityStore} from the global store registry. * @param storeName The {@link EntityStore} name of the instance to be returned. */ -export function getEntityStoreByName, S = TEntityStore extends EntityStore ? T : never>(storeName: string): TEntityStore { +export function getEntityStoreByName, S extends EntityState = TEntityStore extends EntityStore ? T : never>(storeName: string): TEntityStore { return getStoreByName(storeName) as TEntityStore; } @@ -106,7 +106,7 @@ export function runStoreAction, S = TStore extends Store * runEntityStoreAction(BooksStore, EntityStoreAction.SetEntities, set => set([{ id: 1 }, { id: 2 }])); * */ -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction.SetEntities, operation: (operator: TEntityStore['set']) => void @@ -121,7 +121,7 @@ export function runEntityStoreAction, S = TE * runEntityStoreAction(BooksStore, EntityStoreAction.AddEntities, add => add({ id: 1 })); * */ -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction.AddEntities, operation: (operator: TEntityStore['add']) => void @@ -136,7 +136,7 @@ export function runEntityStoreAction, S = TE * runEntityStoreAction(BooksStore, EntityStoreAction.UpdateEntities, update => update(2, { title: 'New title' })); * */ -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction.UpdateEntities, operation: (operator: TEntityStore['update']) => void @@ -151,7 +151,7 @@ export function runEntityStoreAction, S = TE * runEntityStoreAction(BooksStore, EntityStoreAction.RemoveEntities, remove => remove(2)); * */ -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction.RemoveEntities, operation: (operator: TEntityStore['remove']) => void @@ -166,7 +166,7 @@ export function runEntityStoreAction, S = TE * runEntityStoreAction(BooksStore, EntityStoreAction.UpsertEntities, upsert => upsert([2, 3], { title: 'New Title' }, (id, newState) => ({ id, ...newState, price: 0 }))); * */ -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction.UpsertEntities, operation: (operator: TEntityStore['upsert']) => void @@ -183,12 +183,12 @@ export function runEntityStoreAction, S = TE * { id: 4, title: 'Another title', price: 0 }, * )); */ -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction.UpsertManyEntities, operation: (operator: TEntityStore['upsertMany']) => void ); -export function runEntityStoreAction, S = TEntityStore extends EntityStore ? T : any>( +export function runEntityStoreAction, S extends EntityState = TEntityStore extends EntityStore ? T : any>( storeClassOrName: Constructor | string, action: EntityStoreAction, operation: (operator: TEntityStore[keyof TEntityStore] & Function) => void