Skip to content

Commit f80c4b2

Browse files
authored
ArrayStore - onPush TypeScript type does not match actual return value (T1316896) (#32077)
1 parent 05123ad commit f80c4b2

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

e2e/compilation-cases/T1316896.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ArrayStore } from 'devextreme/common/data';
2+
3+
interface Entity {
4+
id?: string;
5+
name?: string;
6+
}
7+
8+
type Change<TItem, TKey> = {
9+
type: 'insert' | 'update' | 'remove';
10+
data?: TItem;
11+
key?: TKey;
12+
index?: number;
13+
};
14+
15+
function notify(changes: Array<Change<Entity, string>>): void {
16+
const change = changes[0];
17+
if (change?.data?.id && change.key) {
18+
const identifier: string = change.key;
19+
const newId: string = change.data.id;
20+
21+
const ensureString: string = `${identifier}:${newId}`;
22+
// eslint-disable-next-line no-console
23+
// console.log(ensureString);
24+
}
25+
}
26+
27+
const store = new ArrayStore<Entity, string>({
28+
key: 'id',
29+
data: [{ id: '1', name: 'Initial' }],
30+
onPush: notify,
31+
});
32+
33+
function load(items: Entity[]): void {
34+
const changes: Array<Change<Entity, string>> = items.map((data): Change<Entity, string> => ({
35+
key: data.id,
36+
data,
37+
type: 'insert',
38+
}));
39+
40+
store.push(changes);
41+
}
42+
43+
function pushSingle(): void {
44+
store.push([
45+
{
46+
key: '2',
47+
data: { id: '2', name: 'New entity' },
48+
type: 'insert',
49+
},
50+
]);
51+
}
52+
53+
load([{ id: '3', name: 'Batch load' }]);
54+
pushSingle();

packages/devextreme/js/data/store.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { DxPromise, DxExtendedPromise } from '../core/utils/deferred';
22
import { DeepPartial } from '../core';
33
import { FilterDescriptor, GroupDescriptor, LoadOptions } from '../common/data';
44

5+
type StoreChange<TItem = any, TKey = any > = {
6+
type: 'insert' | 'update' | 'remove';
7+
data?: DeepPartial<TItem>;
8+
key?: TKey;
9+
index?: number;
10+
};
11+
512
/**
613
* @docid StoreOptions
714
* @namespace DevExpress.common.data
@@ -57,10 +64,11 @@ export type StoreOptions<
5764
onModifying?: Function;
5865
/**
5966
* @docid StoreOptions.onPush
67+
* @type_function_param1 changes:Array<any>
6068
* @action
6169
* @public
6270
*/
63-
onPush?: ((changes: Array<TItem>) => void);
71+
onPush?: ((changes: Array<StoreChange<TItem, TKey>>) => void);
6472
/**
6573
* @docid StoreOptions.onRemoved
6674
* @type_function_param1 key:object|string|number
@@ -166,7 +174,7 @@ export class Store<
166174
* @param1 changes:Array<any>
167175
* @public
168176
*/
169-
push(changes: Array<{ type: 'insert' | 'update' | 'remove'; data?: DeepPartial<TItem>; key?: TKey; index?: number }>): void;
177+
push(changes: Array<StoreChange<TItem, TKey>>): void;
170178
/**
171179
* @docid
172180
* @publicName remove(key)

packages/devextreme/ts/dx.all.d.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,14 +4004,7 @@ declare module DevExpress.common.data {
40044004
/**
40054005
* [descr:Store.push(changes)]
40064006
*/
4007-
push(
4008-
changes: Array<{
4009-
type: 'insert' | 'update' | 'remove';
4010-
data?: DevExpress.core.DeepPartial<TItem>;
4011-
key?: TKey;
4012-
index?: number;
4013-
}>
4014-
): void;
4007+
push(changes: Array<DevExpress.data.StoreChange<TItem, TKey>>): void;
40154008
/**
40164009
* [descr:Store.remove(key)]
40174010
*/
@@ -4067,7 +4060,7 @@ declare module DevExpress.common.data {
40674060
/**
40684061
* [descr:StoreOptions.onPush]
40694062
*/
4070-
onPush?: (changes: Array<TItem>) => void;
4063+
onPush?: (changes: Array<DevExpress.data.StoreChange<TItem, TKey>>) => void;
40714064
/**
40724065
* [descr:StoreOptions.onRemoved]
40734066
*/
@@ -7670,6 +7663,15 @@ declare module DevExpress.data {
76707663
* @deprecated Attention! This type is for internal purposes only. If you used it previously, please submit a ticket to our {@link https://supportcenter.devexpress.com/ticket/create Support Center}. We will check if there is an alternative solution.
76717664
*/
76727665
export type SortDescriptor<T> = KeySelector<T> | OrderingDescriptor<T>;
7666+
/**
7667+
* @deprecated Attention! This type is for internal purposes only. If you used it previously, please submit a ticket to our {@link https://supportcenter.devexpress.com/ticket/create Support Center}. We will check if there is an alternative solution.
7668+
*/
7669+
type StoreChange<TItem = any, TKey = any> = {
7670+
type: 'insert' | 'update' | 'remove';
7671+
data?: DevExpress.core.DeepPartial<TItem>;
7672+
key?: TKey;
7673+
index?: number;
7674+
};
76737675
/**
76747676
* @deprecated Attention! This type is for internal purposes only. If you used it previously, please submit a ticket to our {@link https://supportcenter.devexpress.com/ticket/create Support Center}. We will check if there is an alternative solution.
76757677
*/

0 commit comments

Comments
 (0)