Skip to content

Commit 7cfebef

Browse files
Collection: improve typing for edit, live_update strategies (DevExpress#30278)
1 parent f33f31b commit 7cfebef

File tree

11 files changed

+416
-242
lines changed

11 files changed

+416
-242
lines changed

packages/devextreme/js/__internal/ui/collection/collection_widget.base.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface DataChange<TItem = CollectionItem, TKey = number | string> {
7272
key: TKey;
7373
type: DataChangeType;
7474
data: DeepPartial<TItem>;
75+
index: number;
7576
}
7677

7778
type ItemTemplate<TItem> = template | (
@@ -99,8 +100,7 @@ export interface CollectionWidgetBaseProperties<
99100
TComponent extends CollectionWidget<any, TItem, TKey> | any,
100101
// eslint-disable-next-line @typescript-eslint/no-explicit-any
101102
TItem extends ItemLike = any,
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
TKey = any,
103+
TKey = string | number,
104104
> extends CollectionWidgetOptions<TComponent, TItem, TKey> {
105105
focusedElement?: dxElementWrapper;
106106

@@ -1405,7 +1405,7 @@ class CollectionWidget<
14051405
initiator: dxElementWrapper | Element,
14061406
handler: () => void,
14071407
actionArgs: Record<string, unknown>,
1408-
actionConfig: ActionConfig,
1408+
actionConfig?: ActionConfig,
14091409
): void {
14101410
const action = this._createAction(handler, extend({
14111411
validatingTargetName: 'itemElement',
@@ -1437,8 +1437,7 @@ class CollectionWidget<
14371437
}
14381438

14391439
_getItemData(itemElement: Element | dxElementWrapper): TItem {
1440-
// @ts-expect-error ts-error
1441-
return $(itemElement).data(this._itemDataKey());
1440+
return $(itemElement).data(this._itemDataKey()) as TItem;
14421441
}
14431442

14441443
_getSummaryItemsSize(

packages/devextreme/js/__internal/ui/collection/m_collection_widget.edit.strategy.plain.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { dxElementWrapper } from '@js/core/renderer';
2-
import type { CollectionWidgetItem } from '@js/ui/collection/ui.collection_widget.base';
2+
import type { ItemLike } from '@js/ui/collection/ui.collection_widget.base';
33
import type { CollectionItemIndex } from '@ts/ui/collection/m_collection_widget.edit.strategy';
44
import EditStrategy from '@ts/ui/collection/m_collection_widget.edit.strategy';
55

66
class PlainEditStrategy<
77
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8-
TItem extends CollectionWidgetItem = any,
9-
> extends EditStrategy<TItem> {
8+
TItem extends ItemLike = any,
9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
TKey = any,
11+
> extends EditStrategy<TItem, TKey> {
1012
_getPlainItems(): TItem[] {
1113
return this._getItems() ?? [];
1214
}
@@ -31,16 +33,16 @@ class PlainEditStrategy<
3133
return this._getPlainItems();
3234
}
3335

34-
getKeysByItems(items: TItem[]): (string | number)[] {
36+
getKeysByItems(items: TItem[]): TKey[] {
3537
const keyOf = this._collectionWidget.keyOf.bind(this._collectionWidget);
36-
let result: (string | number | TItem)[] = items;
38+
let result: (TKey | TItem)[] = items;
3739
if (keyOf) {
38-
result = items.map((item) => keyOf(item) as string | number);
40+
result = items.map((item) => keyOf(item));
3941
}
40-
return result as (string | number)[];
42+
return result as TKey[];
4143
}
4244

43-
getIndexByKey(key: string | number): number {
45+
getIndexByKey(key: TKey): number {
4446
const cache = this._cache;
4547
const keys = cache?.keys ?? this.getKeysByItems(this._getPlainItems());
4648

@@ -60,7 +62,7 @@ class PlainEditStrategy<
6062
}
6163

6264
// eslint-disable-next-line class-methods-use-this
63-
getItemsByKeys(keys: (string | number)[], items?: TItem[]): TItem[] {
65+
getItemsByKeys(keys: TKey[], items?: TItem[]): TItem[] {
6466
return (items ?? keys).slice() as TItem[];
6567
}
6668

@@ -99,7 +101,7 @@ class PlainEditStrategy<
99101
}
100102

101103
_getItemByNormalizedIndex(index: number): dxElementWrapper {
102-
// @ts-expect-error - eq() can return null but we handle it appropriately
104+
// @ts-expect-error ts-error
103105
return index > -1 ? this._collectionWidget._itemElements().eq(index) : null;
104106
}
105107

packages/devextreme/js/__internal/ui/collection/m_collection_widget.edit.strategy.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import type { dxElementWrapper } from '@js/core/renderer';
44
import $ from '@js/core/renderer';
55
import { equalByValue } from '@js/core/utils/common';
66
import { isRenderer } from '@js/core/utils/type';
7-
import type { CollectionWidgetItem } from '@js/ui/collection/ui.collection_widget.base';
7+
import type { ItemLike } from '@js/ui/collection/ui.collection_widget.base';
88
import type CollectionWidget from '@ts/ui/collection/m_collection_widget.edit';
99
import type { CollectionWidgetEditProperties } from '@ts/ui/collection/m_collection_widget.edit';
1010

1111
export type CollectionItemIndex = number | { group: number; item: number };
1212

1313
export type EditStrategyComponent<
14-
TItem extends CollectionWidgetItem = CollectionWidgetItem,
14+
TItem extends ItemLike = ItemLike,
15+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16+
TKey = any,
1517
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16-
> = Pick<CollectionWidget<CollectionWidgetEditProperties<any, TItem>, TItem>,
18+
> = Pick<CollectionWidget<CollectionWidgetEditProperties<any, TItem, TKey>, TItem, TKey>,
1719
'keyOf'
1820
| '_isKeySpecified'
1921
| '_itemElements'
@@ -22,19 +24,22 @@ export type EditStrategyComponent<
2224
| '_dataController'
2325
>;
2426

25-
interface KeysCache {
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
interface KeysCache<TKey = any> {
2629
[key: string]: unknown;
27-
keys?: (string | number)[];
30+
keys?: TKey[];
2831
}
2932
class EditStrategy<
3033
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31-
TItem extends CollectionWidgetItem = any,
34+
TItem extends ItemLike = any,
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36+
TKey = any,
3237
> {
33-
_collectionWidget!: EditStrategyComponent<TItem>;
38+
_collectionWidget!: EditStrategyComponent<TItem, TKey>;
3439

35-
_cache?: KeysCache | null;
40+
_cache?: KeysCache<TKey> | null;
3641

37-
constructor(collectionWidget: EditStrategyComponent<TItem>) {
42+
constructor(collectionWidget: EditStrategyComponent<TItem, TKey>) {
3843
this._collectionWidget = collectionWidget;
3944
}
4045

@@ -54,12 +59,12 @@ class EditStrategy<
5459
}
5560

5661
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
57-
getKeysByItems(items: TItem[]): (string | number)[] {
62+
getKeysByItems(items: TItem[]): TKey[] {
5863
return Class.abstract();
5964
}
6065

6166
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
62-
getItemsByKeys(keys: (string | number)[], items?: TItem[]): TItem[] {
67+
getItemsByKeys(keys: TKey[], items?: TItem[]): TItem[] {
6368
return Class.abstract();
6469
}
6570

@@ -68,13 +73,13 @@ class EditStrategy<
6873
return Class.abstract();
6974
}
7075

71-
getKeyByIndex(index: number): string | number {
76+
getKeyByIndex(index: number): TKey {
7277
const resultIndex = this._denormalizeItemIndex(index);
7378

7479
return this.getKeysByItems([this.getItemDataByIndex(resultIndex as number)])[0];
7580
}
7681

77-
_equalKeys(key1: string | number, key2: string | number): boolean {
82+
_equalKeys(key1: TKey, key2: TKey): boolean {
7883
if (this._collectionWidget._isKeySpecified()) {
7984
return equalByValue(key1, key2);
8085
}
@@ -90,7 +95,7 @@ class EditStrategy<
9095
}
9196

9297
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
93-
getIndexByKey(key: string | number): number {
98+
getIndexByKey(key: TKey): number {
9499
return Class.abstract();
95100
}
96101

@@ -147,8 +152,7 @@ class EditStrategy<
147152

148153
// eslint-disable-next-line class-methods-use-this
149154
_isNode(el: unknown): el is Element {
150-
// @ts-expect-error Property 'get' does not exist on type 'unknown'
151-
return domAdapter.isNode(el && isRenderer(el) ? el.get(0) : el);
155+
return domAdapter.isNode(el && isRenderer(el) ? (el as dxElementWrapper).get(0) : el);
152156
}
153157

154158
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this

0 commit comments

Comments
 (0)