Skip to content

Commit 7111501

Browse files
authored
Selection: improve TS typing (#30768)
1 parent 18f72ac commit 7111501

File tree

9 files changed

+795
-360
lines changed

9 files changed

+795
-360
lines changed

packages/devextreme/js/__internal/grids/grid_core/selection/m_selection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ export class SelectionController extends modules.Controller {
299299
private _createSelection() {
300300
const options = this._getSelectionConfig();
301301

302+
// @ts-expect-error TKey
302303
return new Selection(options);
303304
}
304305

packages/devextreme/js/__internal/grids/new/grid_core/selection/controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export class SelectionController {
4545

4646
private readonly selectionOption: ReadonlySignal<SelectionOptions> = this.options.oneWay('selection');
4747

48-
private readonly selectionHelper: ReadonlySignal<Selection | undefined>;
48+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49+
private readonly selectionHelper: ReadonlySignal<Selection<any, any, false> | undefined>;
4950

5051
private readonly _isCheckBoxesRendered = signal<boolean>(false);
5152

@@ -365,6 +366,7 @@ export class SelectionController {
365366
}
366367

367368
public getSelectedCardsData(): DataObject[] {
369+
// @ts-expect-error undefined is not assignable to DataObject[]
368370
return this.selectionHelper?.peek()?.getSelectedItems();
369371
}
370372

packages/devextreme/js/__internal/grids/tree_list/selection/m_selection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ const selection = (Base: ModuleType<SelectionController>) => class SelectionCont
543543
const selectedKeys = this.getSelectedRowKeys(mode) || [];
544544
const selectedRowsData: any[] = [];
545545

546+
// @ts-expect-error selection may be deferred only in DataGrid,
547+
// we need to improve GridCore types to take it into account
546548
selectedKeys.forEach((key) => {
547549
// @ts-expect-error
548550
const node = dataController.getNodeByKey(key);

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
when,
1515
} from '@js/core/utils/deferred';
1616
import { each } from '@js/core/utils/iterator';
17-
import { isDefined } from '@js/core/utils/type';
17+
import { isDefined, isObject } from '@js/core/utils/type';
1818
import type { DxEvent } from '@js/events';
1919
import type { ItemLike, SelectionChangeInfo } from '@js/ui/collection/ui.collection_widget.base';
2020
import errors from '@js/ui/widget/ui.errors';
@@ -76,7 +76,8 @@ class CollectionWidget<
7676
> extends BaseCollectionWidget<TProperties, TItem, TKey> {
7777
static _userOptions = {};
7878

79-
_selection!: Selection;
79+
// @ts-expect-error TItem
80+
_selection!: Selection<TItem, TKey, false>;
8081

8182
_editStrategy!: PlainEditStrategy<TItem, TKey>;
8283

@@ -169,7 +170,7 @@ class CollectionWidget<
169170
return this._editStrategy.getItemsByKeys(selectedItemKeys, selectedItems);
170171
}
171172

172-
_getKeyByIndex(index: CollectionItemIndex): unknown {
173+
_getKeyByIndex(index: CollectionItemIndex): TKey {
173174
return this._editStrategy.getKeyByIndex(index);
174175
}
175176

@@ -224,7 +225,8 @@ class CollectionWidget<
224225
const { itemsGetter } = this._editStrategy;
225226
const { selectionMode, maxFilterLengthInRequest } = this.option();
226227

227-
this._selection = new Selection({
228+
// @ts-expect-error TItem
229+
this._selection = new Selection<TItem, TKey, false>({
228230
allowNullValue: this._nullValueSelectionSupported(),
229231
mode: selectionMode,
230232
maxFilterLengthInRequest,
@@ -259,7 +261,7 @@ class CollectionWidget<
259261
},
260262
key: this.key.bind(this),
261263
keyOf: this.keyOf.bind(this),
262-
load(options): DeferredObj<unknown> {
264+
load(options): DeferredObj<TItem[]> {
263265
const dataController = that._dataController;
264266
options.customQueryParams = dataController.loadOptions()?.customQueryParams;
265267
options.userData = dataController.userData();
@@ -276,7 +278,7 @@ class CollectionWidget<
276278
dataController.applyMapFunction(items);
277279
});
278280
}
279-
return Deferred().resolve(this.plainItems());
281+
return Deferred<TItem[]>().resolve(this.plainItems());
280282
},
281283
// eslint-disable-next-line @stylistic/max-len
282284
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/explicit-function-return-type
@@ -479,7 +481,8 @@ class CollectionWidget<
479481

480482
const { grouped } = this.option();
481483

482-
if (grouped && normalizedSelection?.items) {
484+
const hasSubItems = (item: TItem): item is TItem & { items: TItem[] } => isObject(item) && 'items' in item && Array.isArray(item.items);
485+
if (grouped && hasSubItems(normalizedSelection)) {
483486
normalizedSelection.items = [normalizedSelection.items[0]];
484487
}
485488

0 commit comments

Comments
 (0)