用 Akita 实现基本抽象状态的适当方法
Appropriate way to implement the base abstract state with Akita
我试过实现这样的东西,但我有点困惑。假设我需要实现各种基于网格的页面,这些页面的设计方式大致相同,但每个页面都有一些特点。这就是为什么我需要一些基础抽象 store/query 服务,我计划在其中实现通用逻辑。
每个网格将以 BaseLineItem
中的 child 运行:
export interface BaseListItem {
Id: number;
StatusId: number;
// Other properties
}
我尝试实现它的方式
export interface PageState<TListItem extends BaseListItem> extends EntityState<TListItem> {
// some state properties
}
export abstract class PageStore<TListItem extends BaseListItem, TState extends PageState<TListItem>> extends EntityStore<TState> {
constructor(initialState: Partial<TState>) {
super(initialState);
}
addNewItems(items: TListItem[]): void {
this.upsertMany(items);
}
}
假设我将为每个表单实施 children 订单项、状态和商店,因此我可以添加一些特定于每个表单的特性。但是在那个地方,我在 addNewItems
方法中遇到了一个问题 - upsertMany
,它显示了以下错误:
Argument of type 'TListItem[]' is not assignable to parameter of type 'getEntityType<TState>[]'.\n Type 'TListItem' is not assignable to type 'getEntityType<TState>'.\n Type 'BaseListItem' is not assignable to type 'getEntityType<TState>'.
Akita 似乎无法推断实体的类型。
就是这个问题。如何在不到处使用 as any
的情况下解决这个问题(如果我在最基础的级别开始这样做,我将在源代码中丢失智能感知)?我是从.Net背景来到front-end世界的。也许我不明白什么,这种架构模式在这里不是最好的?我需要一些建议。
EntityStore.upsertMany 方法将 EntityType[]
的参数数组作为参数数组,这是使用打字稿助手类型
推断出来的
export class EntityStore<S extends EntityState = any, EntityType = getEntityType<S>,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是这个助手的the source code:
export type getEntityType<S> = S extends EntityState<infer I> ? I : never;
Constrained generics,你在你的实现中使用,混淆了 helper helper 一点,它不能推断正确的类型和 returns unknown
这与你的不匹配通用类型。
也许 TypeScript 将来会针对此类情况更好地推断类型,但目前一个简单的解决方案是在您的代码中使用相同的 Akita 助手而不是 TListItem[]
:
addNewItems(items: getEntityType<TState>[]): void {
this.upsertMany(items);
}
你可以在Playground
中测试
下面是派生的示例 class,您可以在其中观察到正确的类型推断。
此外,您可以查看 Akita issue tracker
中的类似问题
我试过实现这样的东西,但我有点困惑。假设我需要实现各种基于网格的页面,这些页面的设计方式大致相同,但每个页面都有一些特点。这就是为什么我需要一些基础抽象 store/query 服务,我计划在其中实现通用逻辑。
每个网格将以 BaseLineItem
中的 child 运行:
export interface BaseListItem {
Id: number;
StatusId: number;
// Other properties
}
我尝试实现它的方式
export interface PageState<TListItem extends BaseListItem> extends EntityState<TListItem> {
// some state properties
}
export abstract class PageStore<TListItem extends BaseListItem, TState extends PageState<TListItem>> extends EntityStore<TState> {
constructor(initialState: Partial<TState>) {
super(initialState);
}
addNewItems(items: TListItem[]): void {
this.upsertMany(items);
}
}
假设我将为每个表单实施 children 订单项、状态和商店,因此我可以添加一些特定于每个表单的特性。但是在那个地方,我在 addNewItems
方法中遇到了一个问题 - upsertMany
,它显示了以下错误:
Argument of type 'TListItem[]' is not assignable to parameter of type 'getEntityType<TState>[]'.\n Type 'TListItem' is not assignable to type 'getEntityType<TState>'.\n Type 'BaseListItem' is not assignable to type 'getEntityType<TState>'.
Akita 似乎无法推断实体的类型。
就是这个问题。如何在不到处使用 as any
的情况下解决这个问题(如果我在最基础的级别开始这样做,我将在源代码中丢失智能感知)?我是从.Net背景来到front-end世界的。也许我不明白什么,这种架构模式在这里不是最好的?我需要一些建议。
EntityStore.upsertMany 方法将 EntityType[]
的参数数组作为参数数组,这是使用打字稿助手类型
export class EntityStore<S extends EntityState = any, EntityType = getEntityType<S>,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是这个助手的the source code:
export type getEntityType<S> = S extends EntityState<infer I> ? I : never;
Constrained generics,你在你的实现中使用,混淆了 helper helper 一点,它不能推断正确的类型和 returns unknown
这与你的不匹配通用类型。
也许 TypeScript 将来会针对此类情况更好地推断类型,但目前一个简单的解决方案是在您的代码中使用相同的 Akita 助手而不是 TListItem[]
:
addNewItems(items: getEntityType<TState>[]): void {
this.upsertMany(items);
}
你可以在Playground
中测试下面是派生的示例 class,您可以在其中观察到正确的类型推断。
此外,您可以查看 Akita issue tracker
中的类似问题