@wordpress/core-data 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/selectors.ts CHANGED
@@ -18,6 +18,65 @@ import { STORE_NAME } from './name';
18
18
  import { getQueriedItems } from './queried-data';
19
19
  import { DEFAULT_ENTITY_KEY } from './entities';
20
20
  import { getNormalizedCommaSeparable, isRawAttribute } from './utils';
21
+ import type { Context, User, Theme, WpTemplate } from './entity-types';
22
+
23
+ // This is an incomplete, high-level approximation of the State type.
24
+ // It makes the selectors slightly more safe, but is intended to evolve
25
+ // into a more detailed representation over time.
26
+ // See https://github.com/WordPress/gutenberg/pull/40025#discussion_r865410589 for more context.
27
+ interface State {
28
+ autosaves: Record< string | number, Array< unknown > >;
29
+ blockPatterns: Array< unknown >;
30
+ blockPatternCategories: Array< unknown >;
31
+ currentGlobalStylesId: string;
32
+ currentTheme: Theme< 'edit' >;
33
+ currentUser: User< 'edit' >;
34
+ embedPreviews: Record< string, { html: string } >;
35
+ entities: EntitiesState;
36
+ themeBaseGlobalStyles: Record< string, Object >;
37
+ themeGlobalStyleVariations: Record< string, string >;
38
+ undo: UndoState;
39
+ users: UserState;
40
+ }
41
+
42
+ interface EntitiesState {
43
+ config: EntityConfig[];
44
+ records: Record< string, unknown >;
45
+ }
46
+
47
+ interface EntityConfig {
48
+ name: string;
49
+ kind: string;
50
+ }
51
+
52
+ interface UndoState extends Array< Object > {
53
+ flattenedUndo: unknown;
54
+ offset: number;
55
+ }
56
+
57
+ interface UserState {
58
+ queries: Record< string, RecordKey[] >;
59
+ byId: Record< RecordKey, User< 'edit' > >;
60
+ }
61
+
62
+ type RecordKey = number | string;
63
+ type EntityRecord = any;
64
+ type Optional< T > = T | undefined;
65
+
66
+ /**
67
+ * HTTP Query parameters sent with the API request to fetch the entity records.
68
+ */
69
+ export type EntityQuery<
70
+ C extends Context,
71
+ Fields extends string[] | undefined = undefined
72
+ > = Record< string, any > & {
73
+ context?: C;
74
+ /**
75
+ * The requested fields. If specified, the REST API will remove from the response
76
+ * any fields not on that list.
77
+ */
78
+ _fields?: Fields;
79
+ };
21
80
 
22
81
  /**
23
82
  * Shared reference to an empty object for cases where it is important to avoid
@@ -32,13 +91,13 @@ const EMPTY_OBJECT = {};
32
91
  * Returns true if a request is in progress for embed preview data, or false
33
92
  * otherwise.
34
93
  *
35
- * @param {Object} state Data state.
36
- * @param {string} url URL the preview would be for.
94
+ * @param state Data state.
95
+ * @param url URL the preview would be for.
37
96
  *
38
- * @return {boolean} Whether a request is in progress for an embed preview.
97
+ * @return Whether a request is in progress for an embed preview.
39
98
  */
40
99
  export const isRequestingEmbedPreview = createRegistrySelector(
41
- ( select ) => ( state, url ) => {
100
+ ( select ) => ( state: State, url: string ): boolean => {
42
101
  return select( STORE_NAME ).isResolving( 'getEmbedPreview', [ url ] );
43
102
  }
44
103
  );
@@ -48,12 +107,15 @@ export const isRequestingEmbedPreview = createRegistrySelector(
48
107
  *
49
108
  * @deprecated since 11.3. Callers should use `select( 'core' ).getUsers({ who: 'authors' })` instead.
50
109
  *
51
- * @param {Object} state Data state.
52
- * @param {Object|undefined} query Optional object of query parameters to
53
- * include with request.
54
- * @return {Array} Authors list.
110
+ * @param state Data state.
111
+ * @param query Optional object of query parameters to
112
+ * include with request.
113
+ * @return Authors list.
55
114
  */
56
- export function getAuthors( state, query ) {
115
+ export function getAuthors(
116
+ state: State,
117
+ query?: EntityQuery< any >
118
+ ): User< 'edit' >[] {
57
119
  deprecated( "select( 'core' ).getAuthors()", {
58
120
  since: '5.9',
59
121
  alternative: "select( 'core' ).getUsers({ who: 'authors' })",
@@ -69,41 +131,44 @@ export function getAuthors( state, query ) {
69
131
  /**
70
132
  * Returns the current user.
71
133
  *
72
- * @param {Object} state Data state.
134
+ * @param state Data state.
73
135
  *
74
- * @return {Object} Current user object.
136
+ * @return Current user object.
75
137
  */
76
- export function getCurrentUser( state ) {
138
+ export function getCurrentUser( state: State ): User< 'edit' > {
77
139
  return state.currentUser;
78
140
  }
79
141
 
80
142
  /**
81
143
  * Returns all the users returned by a query ID.
82
144
  *
83
- * @param {Object} state Data state.
84
- * @param {string} queryID Query ID.
145
+ * @param state Data state.
146
+ * @param queryID Query ID.
85
147
  *
86
- * @return {Array} Users list.
148
+ * @return Users list.
87
149
  */
88
150
  export const getUserQueryResults = createSelector(
89
- ( state, queryID ) => {
151
+ ( state: State, queryID: string ): User< 'edit' >[] => {
90
152
  const queryResults = state.users.queries[ queryID ];
91
153
 
92
154
  return map( queryResults, ( id ) => state.users.byId[ id ] );
93
155
  },
94
- ( state, queryID ) => [ state.users.queries[ queryID ], state.users.byId ]
156
+ ( state: State, queryID: string ) => [
157
+ state.users.queries[ queryID ],
158
+ state.users.byId,
159
+ ]
95
160
  );
96
161
 
97
162
  /**
98
163
  * Returns the loaded entities for the given kind.
99
164
  *
100
165
  * @deprecated since WordPress 6.0. Use getEntitiesConfig instead
101
- * @param {Object} state Data state.
102
- * @param {string} kind Entity kind.
166
+ * @param state Data state.
167
+ * @param kind Entity kind.
103
168
  *
104
- * @return {Array<Object>} Array of entities with config matching kind.
169
+ * @return Array of entities with config matching kind.
105
170
  */
106
- export function getEntitiesByKind( state, kind ) {
171
+ export function getEntitiesByKind( state: State, kind: string ): Array< any > {
107
172
  deprecated( "wp.data.select( 'core' ).getEntitiesByKind()", {
108
173
  since: '6.0',
109
174
  alternative: "wp.data.select( 'core' ).getEntitiesConfig()",
@@ -114,12 +179,12 @@ export function getEntitiesByKind( state, kind ) {
114
179
  /**
115
180
  * Returns the loaded entities for the given kind.
116
181
  *
117
- * @param {Object} state Data state.
118
- * @param {string} kind Entity kind.
182
+ * @param state Data state.
183
+ * @param kind Entity kind.
119
184
  *
120
- * @return {Array<Object>} Array of entities with config matching kind.
185
+ * @return Array of entities with config matching kind.
121
186
  */
122
- export function getEntitiesConfig( state, kind ) {
187
+ export function getEntitiesConfig( state: State, kind: string ): Array< any > {
123
188
  return filter( state.entities.config, { kind } );
124
189
  }
125
190
 
@@ -127,13 +192,13 @@ export function getEntitiesConfig( state, kind ) {
127
192
  * Returns the entity config given its kind and name.
128
193
  *
129
194
  * @deprecated since WordPress 6.0. Use getEntityConfig instead
130
- * @param {Object} state Data state.
131
- * @param {string} kind Entity kind.
132
- * @param {string} name Entity name.
195
+ * @param state Data state.
196
+ * @param kind Entity kind.
197
+ * @param name Entity name.
133
198
  *
134
- * @return {Object} Entity config
199
+ * @return Entity config
135
200
  */
136
- export function getEntity( state, kind, name ) {
201
+ export function getEntity( state: State, kind: string, name: string ): any {
137
202
  deprecated( "wp.data.select( 'core' ).getEntity()", {
138
203
  since: '6.0',
139
204
  alternative: "wp.data.select( 'core' ).getEntityConfig()",
@@ -144,13 +209,17 @@ export function getEntity( state, kind, name ) {
144
209
  /**
145
210
  * Returns the entity config given its kind and name.
146
211
  *
147
- * @param {Object} state Data state.
148
- * @param {string} kind Entity kind.
149
- * @param {string} name Entity name.
212
+ * @param state Data state.
213
+ * @param kind Entity kind.
214
+ * @param name Entity name.
150
215
  *
151
- * @return {Object} Entity config
216
+ * @return Entity config
152
217
  */
153
- export function getEntityConfig( state, kind, name ) {
218
+ export function getEntityConfig(
219
+ state: State,
220
+ kind: string,
221
+ name: string
222
+ ): any {
154
223
  return find( state.entities.config, { kind, name } );
155
224
  }
156
225
 
@@ -159,16 +228,22 @@ export function getEntityConfig( state, kind, name ) {
159
228
  * yet received, undefined if the value entity is known to not exist, or the
160
229
  * entity object if it exists and is received.
161
230
  *
162
- * @param {Object} state State tree
163
- * @param {string} kind Entity kind.
164
- * @param {string} name Entity name.
165
- * @param {number} key Record's key
166
- * @param {?Object} query Optional query.
231
+ * @param state State tree
232
+ * @param kind Entity kind.
233
+ * @param name Entity name.
234
+ * @param key Record's key
235
+ * @param query Optional query.
167
236
  *
168
- * @return {Object|undefined} Record.
237
+ * @return Record.
169
238
  */
170
239
  export const getEntityRecord = createSelector(
171
- ( state, kind, name, key, query ) => {
240
+ (
241
+ state: State,
242
+ kind: string,
243
+ name: string,
244
+ key: RecordKey,
245
+ query?: EntityQuery< any >
246
+ ): EntityRecord | undefined => {
172
247
  const queriedState = get( state.entities.records, [
173
248
  kind,
174
249
  name,
@@ -202,7 +277,13 @@ export const getEntityRecord = createSelector(
202
277
 
203
278
  return item;
204
279
  },
205
- ( state, kind, name, recordId, query ) => {
280
+ (
281
+ state: State,
282
+ kind: string,
283
+ name: string,
284
+ recordId: RecordKey,
285
+ query?: EntityQuery< any >
286
+ ) => {
206
287
  const context = query?.context ?? 'default';
207
288
  return [
208
289
  get( state.entities.records, [
@@ -228,19 +309,19 @@ export const getEntityRecord = createSelector(
228
309
  /**
229
310
  * Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity records from the API if the entity record isn't available in the local state.
230
311
  *
231
- * @param {Object} state State tree
232
- * @param {string} kind Entity kind.
233
- * @param {string} name Entity name.
234
- * @param {number} key Record's key
312
+ * @param state State tree
313
+ * @param kind Entity kind.
314
+ * @param name Entity name.
315
+ * @param key Record's key
235
316
  *
236
- * @return {Object|null} Record.
317
+ * @return Record.
237
318
  */
238
319
  export function __experimentalGetEntityRecordNoResolver(
239
- state,
240
- kind,
241
- name,
242
- key
243
- ) {
320
+ state: State,
321
+ kind: string,
322
+ name: string,
323
+ key: RecordKey
324
+ ): EntityRecord | null {
244
325
  return getEntityRecord( state, kind, name, key );
245
326
  }
246
327
 
@@ -248,15 +329,20 @@ export function __experimentalGetEntityRecordNoResolver(
248
329
  * Returns the entity's record object by key,
249
330
  * with its attributes mapped to their raw values.
250
331
  *
251
- * @param {Object} state State tree.
252
- * @param {string} kind Entity kind.
253
- * @param {string} name Entity name.
254
- * @param {number} key Record's key.
332
+ * @param state State tree.
333
+ * @param kind Entity kind.
334
+ * @param name Entity name.
335
+ * @param key Record's key.
255
336
  *
256
- * @return {Object?} Object with the entity's raw attributes.
337
+ * @return Object with the entity's raw attributes.
257
338
  */
258
339
  export const getRawEntityRecord = createSelector(
259
- ( state, kind, name, key ) => {
340
+ (
341
+ state: State,
342
+ kind: string,
343
+ name: string,
344
+ key: RecordKey
345
+ ): EntityRecord | undefined => {
260
346
  const record = getEntityRecord( state, kind, name, key );
261
347
  return (
262
348
  record &&
@@ -279,7 +365,13 @@ export const getRawEntityRecord = createSelector(
279
365
  }, {} )
280
366
  );
281
367
  },
282
- ( state, kind, name, recordId, query ) => {
368
+ (
369
+ state: State,
370
+ kind: string,
371
+ name: string,
372
+ recordId: RecordKey,
373
+ query?: EntityQuery< any >
374
+ ) => {
283
375
  const context = query?.context ?? 'default';
284
376
  return [
285
377
  state.entities.config,
@@ -307,28 +399,38 @@ export const getRawEntityRecord = createSelector(
307
399
  * Returns true if records have been received for the given set of parameters,
308
400
  * or false otherwise.
309
401
  *
310
- * @param {Object} state State tree
311
- * @param {string} kind Entity kind.
312
- * @param {string} name Entity name.
313
- * @param {?Object} query Optional terms query.
402
+ * @param state State tree
403
+ * @param kind Entity kind.
404
+ * @param name Entity name.
405
+ * @param query Optional terms query.
314
406
  *
315
- * @return {boolean} Whether entity records have been received.
407
+ * @return Whether entity records have been received.
316
408
  */
317
- export function hasEntityRecords( state, kind, name, query ) {
409
+ export function hasEntityRecords(
410
+ state: State,
411
+ kind: string,
412
+ name: string,
413
+ query?: EntityQuery< any >
414
+ ): boolean {
318
415
  return Array.isArray( getEntityRecords( state, kind, name, query ) );
319
416
  }
320
417
 
321
418
  /**
322
419
  * Returns the Entity's records.
323
420
  *
324
- * @param {Object} state State tree
325
- * @param {string} kind Entity kind.
326
- * @param {string} name Entity name.
327
- * @param {?Object} query Optional terms query.
421
+ * @param state State tree
422
+ * @param kind Entity kind.
423
+ * @param name Entity name.
424
+ * @param query Optional terms query.
328
425
  *
329
- * @return {?Array} Records.
426
+ * @return Records.
330
427
  */
331
- export function getEntityRecords( state, kind, name, query ) {
428
+ export function getEntityRecords(
429
+ state: State,
430
+ kind: string,
431
+ name: string,
432
+ query?: EntityQuery< any >
433
+ ): Array< EntityRecord > | undefined {
332
434
  // Queried data state is prepopulated for all known entities. If this is not
333
435
  // assigned for the given parameters, then it is known to not exist.
334
436
  const queriedState = get( state.entities.records, [
@@ -342,15 +444,21 @@ export function getEntityRecords( state, kind, name, query ) {
342
444
  return getQueriedItems( queriedState, query );
343
445
  }
344
446
 
447
+ type DirtyEntityRecord = {
448
+ title: string;
449
+ key: RecordKey;
450
+ name: string;
451
+ kind: string;
452
+ };
345
453
  /**
346
454
  * Returns the list of dirty entity records.
347
455
  *
348
- * @param {Object} state State tree.
456
+ * @param state State tree.
349
457
  *
350
- * @return {[{ title: string, key: string, name: string, kind: string }]} The list of updated records
458
+ * @return The list of updated records
351
459
  */
352
460
  export const __experimentalGetDirtyEntityRecords = createSelector(
353
- ( state ) => {
461
+ ( state: State ): Array< DirtyEntityRecord > => {
354
462
  const {
355
463
  entities: { records },
356
464
  } = state;
@@ -360,7 +468,7 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
360
468
  const primaryKeys = Object.keys(
361
469
  records[ kind ][ name ].edits
362
470
  ).filter(
363
- ( primaryKey ) =>
471
+ ( primaryKey: RecordKey ) =>
364
472
  // The entity record must exist (not be deleted),
365
473
  // and it must have edits.
366
474
  getEntityRecord( state, kind, name, primaryKey ) &&
@@ -401,12 +509,12 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
401
509
  /**
402
510
  * Returns the list of entities currently being saved.
403
511
  *
404
- * @param {Object} state State tree.
512
+ * @param state State tree.
405
513
  *
406
- * @return {[{ title: string, key: string, name: string, kind: string }]} The list of records being saved.
514
+ * @return The list of records being saved.
407
515
  */
408
516
  export const __experimentalGetEntitiesBeingSaved = createSelector(
409
- ( state ) => {
517
+ ( state: State ): Array< DirtyEntityRecord > => {
410
518
  const {
411
519
  entities: { records },
412
520
  } = state;
@@ -452,14 +560,19 @@ export const __experimentalGetEntitiesBeingSaved = createSelector(
452
560
  /**
453
561
  * Returns the specified entity record's edits.
454
562
  *
455
- * @param {Object} state State tree.
456
- * @param {string} kind Entity kind.
457
- * @param {string} name Entity name.
458
- * @param {number} recordId Record ID.
563
+ * @param state State tree.
564
+ * @param kind Entity kind.
565
+ * @param name Entity name.
566
+ * @param recordId Record ID.
459
567
  *
460
- * @return {Object?} The entity record's edits.
568
+ * @return The entity record's edits.
461
569
  */
462
- export function getEntityRecordEdits( state, kind, name, recordId ) {
570
+ export function getEntityRecordEdits(
571
+ state: State,
572
+ kind: string,
573
+ name: string,
574
+ recordId: RecordKey
575
+ ): Optional< any > {
463
576
  return get( state.entities.records, [ kind, name, 'edits', recordId ] );
464
577
  }
465
578
 
@@ -470,15 +583,20 @@ export function getEntityRecordEdits( state, kind, name, recordId ) {
470
583
  * are not considered for change detection.
471
584
  * They are defined in the entity's config.
472
585
  *
473
- * @param {Object} state State tree.
474
- * @param {string} kind Entity kind.
475
- * @param {string} name Entity name.
476
- * @param {number} recordId Record ID.
586
+ * @param state State tree.
587
+ * @param kind Entity kind.
588
+ * @param name Entity name.
589
+ * @param recordId Record ID.
477
590
  *
478
- * @return {Object?} The entity record's non transient edits.
591
+ * @return The entity record's non transient edits.
479
592
  */
480
593
  export const getEntityRecordNonTransientEdits = createSelector(
481
- ( state, kind, name, recordId ) => {
594
+ (
595
+ state: State,
596
+ kind: string,
597
+ name: string,
598
+ recordId: RecordKey
599
+ ): Optional< any > => {
482
600
  const { transientEdits } = getEntityConfig( state, kind, name ) || {};
483
601
  const edits = getEntityRecordEdits( state, kind, name, recordId ) || {};
484
602
  if ( ! transientEdits ) {
@@ -491,7 +609,7 @@ export const getEntityRecordNonTransientEdits = createSelector(
491
609
  return acc;
492
610
  }, {} );
493
611
  },
494
- ( state, kind, name, recordId ) => [
612
+ ( state: State, kind: string, name: string, recordId: RecordKey ) => [
495
613
  state.entities.config,
496
614
  get( state.entities.records, [ kind, name, 'edits', recordId ] ),
497
615
  ]
@@ -501,14 +619,19 @@ export const getEntityRecordNonTransientEdits = createSelector(
501
619
  * Returns true if the specified entity record has edits,
502
620
  * and false otherwise.
503
621
  *
504
- * @param {Object} state State tree.
505
- * @param {string} kind Entity kind.
506
- * @param {string} name Entity name.
507
- * @param {number|string} recordId Record ID.
622
+ * @param state State tree.
623
+ * @param kind Entity kind.
624
+ * @param name Entity name.
625
+ * @param recordId Record ID.
508
626
  *
509
- * @return {boolean} Whether the entity record has edits or not.
627
+ * @return Whether the entity record has edits or not.
510
628
  */
511
- export function hasEditsForEntityRecord( state, kind, name, recordId ) {
629
+ export function hasEditsForEntityRecord(
630
+ state: State,
631
+ kind: string,
632
+ name: string,
633
+ recordId: RecordKey
634
+ ): boolean {
512
635
  return (
513
636
  isSavingEntityRecord( state, kind, name, recordId ) ||
514
637
  Object.keys(
@@ -520,19 +643,30 @@ export function hasEditsForEntityRecord( state, kind, name, recordId ) {
520
643
  /**
521
644
  * Returns the specified entity record, merged with its edits.
522
645
  *
523
- * @param {Object} state State tree.
524
- * @param {string} kind Entity kind.
525
- * @param {string} name Entity name.
526
- * @param {number|string} recordId Record ID.
646
+ * @param state State tree.
647
+ * @param kind Entity kind.
648
+ * @param name Entity name.
649
+ * @param recordId Record ID.
527
650
  *
528
- * @return {Object?} The entity record, merged with its edits.
651
+ * @return The entity record, merged with its edits.
529
652
  */
530
653
  export const getEditedEntityRecord = createSelector(
531
- ( state, kind, name, recordId ) => ( {
654
+ (
655
+ state: State,
656
+ kind: string,
657
+ name: string,
658
+ recordId: RecordKey
659
+ ): EntityRecord | undefined => ( {
532
660
  ...getRawEntityRecord( state, kind, name, recordId ),
533
661
  ...getEntityRecordEdits( state, kind, name, recordId ),
534
662
  } ),
535
- ( state, kind, name, recordId, query ) => {
663
+ (
664
+ state: State,
665
+ kind: string,
666
+ name: string,
667
+ recordId: RecordKey,
668
+ query?: EntityQuery< any >
669
+ ) => {
536
670
  const context = query?.context ?? 'default';
537
671
  return [
538
672
  state.entities.config,
@@ -560,14 +694,19 @@ export const getEditedEntityRecord = createSelector(
560
694
  /**
561
695
  * Returns true if the specified entity record is autosaving, and false otherwise.
562
696
  *
563
- * @param {Object} state State tree.
564
- * @param {string} kind Entity kind.
565
- * @param {string} name Entity name.
566
- * @param {number} recordId Record ID.
697
+ * @param state State tree.
698
+ * @param kind Entity kind.
699
+ * @param name Entity name.
700
+ * @param recordId Record ID.
567
701
  *
568
- * @return {boolean} Whether the entity record is autosaving or not.
702
+ * @return Whether the entity record is autosaving or not.
569
703
  */
570
- export function isAutosavingEntityRecord( state, kind, name, recordId ) {
704
+ export function isAutosavingEntityRecord(
705
+ state: State,
706
+ kind: string,
707
+ name: string,
708
+ recordId: RecordKey
709
+ ): boolean {
571
710
  const { pending, isAutosave } = get(
572
711
  state.entities.records,
573
712
  [ kind, name, 'saving', recordId ],
@@ -579,14 +718,19 @@ export function isAutosavingEntityRecord( state, kind, name, recordId ) {
579
718
  /**
580
719
  * Returns true if the specified entity record is saving, and false otherwise.
581
720
  *
582
- * @param {Object} state State tree.
583
- * @param {string} kind Entity kind.
584
- * @param {string} name Entity name.
585
- * @param {number|string} recordId Record ID.
721
+ * @param state State tree.
722
+ * @param kind Entity kind.
723
+ * @param name Entity name.
724
+ * @param recordId Record ID.
586
725
  *
587
- * @return {boolean} Whether the entity record is saving or not.
726
+ * @return Whether the entity record is saving or not.
588
727
  */
589
- export function isSavingEntityRecord( state, kind, name, recordId ) {
728
+ export function isSavingEntityRecord(
729
+ state: State,
730
+ kind: string,
731
+ name: string,
732
+ recordId: RecordKey
733
+ ): boolean {
590
734
  return get(
591
735
  state.entities.records,
592
736
  [ kind, name, 'saving', recordId, 'pending' ],
@@ -597,14 +741,19 @@ export function isSavingEntityRecord( state, kind, name, recordId ) {
597
741
  /**
598
742
  * Returns true if the specified entity record is deleting, and false otherwise.
599
743
  *
600
- * @param {Object} state State tree.
601
- * @param {string} kind Entity kind.
602
- * @param {string} name Entity name.
603
- * @param {number} recordId Record ID.
744
+ * @param state State tree.
745
+ * @param kind Entity kind.
746
+ * @param name Entity name.
747
+ * @param recordId Record ID.
604
748
  *
605
- * @return {boolean} Whether the entity record is deleting or not.
749
+ * @return Whether the entity record is deleting or not.
606
750
  */
607
- export function isDeletingEntityRecord( state, kind, name, recordId ) {
751
+ export function isDeletingEntityRecord(
752
+ state: State,
753
+ kind: string,
754
+ name: string,
755
+ recordId: RecordKey
756
+ ): boolean {
608
757
  return get(
609
758
  state.entities.records,
610
759
  [ kind, name, 'deleting', recordId, 'pending' ],
@@ -615,14 +764,19 @@ export function isDeletingEntityRecord( state, kind, name, recordId ) {
615
764
  /**
616
765
  * Returns the specified entity record's last save error.
617
766
  *
618
- * @param {Object} state State tree.
619
- * @param {string} kind Entity kind.
620
- * @param {string} name Entity name.
621
- * @param {number} recordId Record ID.
767
+ * @param state State tree.
768
+ * @param kind Entity kind.
769
+ * @param name Entity name.
770
+ * @param recordId Record ID.
622
771
  *
623
- * @return {Object?} The entity record's save error.
772
+ * @return The entity record's save error.
624
773
  */
625
- export function getLastEntitySaveError( state, kind, name, recordId ) {
774
+ export function getLastEntitySaveError(
775
+ state: State,
776
+ kind: string,
777
+ name: string,
778
+ recordId: RecordKey
779
+ ): any {
626
780
  return get( state.entities.records, [
627
781
  kind,
628
782
  name,
@@ -635,14 +789,19 @@ export function getLastEntitySaveError( state, kind, name, recordId ) {
635
789
  /**
636
790
  * Returns the specified entity record's last delete error.
637
791
  *
638
- * @param {Object} state State tree.
639
- * @param {string} kind Entity kind.
640
- * @param {string} name Entity name.
641
- * @param {number} recordId Record ID.
792
+ * @param state State tree.
793
+ * @param kind Entity kind.
794
+ * @param name Entity name.
795
+ * @param recordId Record ID.
642
796
  *
643
- * @return {Object?} The entity record's save error.
797
+ * @return The entity record's save error.
644
798
  */
645
- export function getLastEntityDeleteError( state, kind, name, recordId ) {
799
+ export function getLastEntityDeleteError(
800
+ state: State,
801
+ kind: string,
802
+ name: string,
803
+ recordId: RecordKey
804
+ ): any {
646
805
  return get( state.entities.records, [
647
806
  kind,
648
807
  name,
@@ -659,11 +818,11 @@ export function getLastEntityDeleteError( state, kind, name, recordId ) {
659
818
  * of the history stack we are at. 0 is the
660
819
  * last edit, -1 is the second last, and so on.
661
820
  *
662
- * @param {Object} state State tree.
821
+ * @param state State tree.
663
822
  *
664
- * @return {number} The current undo offset.
823
+ * @return The current undo offset.
665
824
  */
666
- function getCurrentUndoOffset( state ) {
825
+ function getCurrentUndoOffset( state: State ): number {
667
826
  return state.undo.offset;
668
827
  }
669
828
 
@@ -671,11 +830,11 @@ function getCurrentUndoOffset( state ) {
671
830
  * Returns the previous edit from the current undo offset
672
831
  * for the entity records edits history, if any.
673
832
  *
674
- * @param {Object} state State tree.
833
+ * @param state State tree.
675
834
  *
676
- * @return {Object?} The edit.
835
+ * @return The edit.
677
836
  */
678
- export function getUndoEdit( state ) {
837
+ export function getUndoEdit( state: State ): Optional< any > {
679
838
  return state.undo[ state.undo.length - 2 + getCurrentUndoOffset( state ) ];
680
839
  }
681
840
 
@@ -683,11 +842,11 @@ export function getUndoEdit( state ) {
683
842
  * Returns the next edit from the current undo offset
684
843
  * for the entity records edits history, if any.
685
844
  *
686
- * @param {Object} state State tree.
845
+ * @param state State tree.
687
846
  *
688
- * @return {Object?} The edit.
847
+ * @return The edit.
689
848
  */
690
- export function getRedoEdit( state ) {
849
+ export function getRedoEdit( state: State ): Optional< any > {
691
850
  return state.undo[ state.undo.length + getCurrentUndoOffset( state ) ];
692
851
  }
693
852
 
@@ -695,11 +854,11 @@ export function getRedoEdit( state ) {
695
854
  * Returns true if there is a previous edit from the current undo offset
696
855
  * for the entity records edits history, and false otherwise.
697
856
  *
698
- * @param {Object} state State tree.
857
+ * @param state State tree.
699
858
  *
700
- * @return {boolean} Whether there is a previous edit or not.
859
+ * @return Whether there is a previous edit or not.
701
860
  */
702
- export function hasUndo( state ) {
861
+ export function hasUndo( state: State ): boolean {
703
862
  return Boolean( getUndoEdit( state ) );
704
863
  }
705
864
 
@@ -707,56 +866,56 @@ export function hasUndo( state ) {
707
866
  * Returns true if there is a next edit from the current undo offset
708
867
  * for the entity records edits history, and false otherwise.
709
868
  *
710
- * @param {Object} state State tree.
869
+ * @param state State tree.
711
870
  *
712
- * @return {boolean} Whether there is a next edit or not.
871
+ * @return Whether there is a next edit or not.
713
872
  */
714
- export function hasRedo( state ) {
873
+ export function hasRedo( state: State ): boolean {
715
874
  return Boolean( getRedoEdit( state ) );
716
875
  }
717
876
 
718
877
  /**
719
878
  * Return the current theme.
720
879
  *
721
- * @param {Object} state Data state.
880
+ * @param state Data state.
722
881
  *
723
- * @return {Object} The current theme.
882
+ * @return The current theme.
724
883
  */
725
- export function getCurrentTheme( state ) {
884
+ export function getCurrentTheme( state: State ): any {
726
885
  return getEntityRecord( state, 'root', 'theme', state.currentTheme );
727
886
  }
728
887
 
729
888
  /**
730
889
  * Return the ID of the current global styles object.
731
890
  *
732
- * @param {Object} state Data state.
891
+ * @param state Data state.
733
892
  *
734
- * @return {string} The current global styles ID.
893
+ * @return The current global styles ID.
735
894
  */
736
- export function __experimentalGetCurrentGlobalStylesId( state ) {
895
+ export function __experimentalGetCurrentGlobalStylesId( state: State ): string {
737
896
  return state.currentGlobalStylesId;
738
897
  }
739
898
 
740
899
  /**
741
900
  * Return theme supports data in the index.
742
901
  *
743
- * @param {Object} state Data state.
902
+ * @param state Data state.
744
903
  *
745
- * @return {*} Index data.
904
+ * @return Index data.
746
905
  */
747
- export function getThemeSupports( state ) {
906
+ export function getThemeSupports( state: State ): any {
748
907
  return getCurrentTheme( state )?.theme_supports ?? EMPTY_OBJECT;
749
908
  }
750
909
 
751
910
  /**
752
911
  * Returns the embed preview for the given URL.
753
912
  *
754
- * @param {Object} state Data state.
755
- * @param {string} url Embedded URL.
913
+ * @param state Data state.
914
+ * @param url Embedded URL.
756
915
  *
757
- * @return {*} Undefined if the preview has not been fetched, otherwise, the preview fetched from the embed preview API.
916
+ * @return Undefined if the preview has not been fetched, otherwise, the preview fetched from the embed preview API.
758
917
  */
759
- export function getEmbedPreview( state, url ) {
918
+ export function getEmbedPreview( state: State, url: string ): any {
760
919
  return state.embedPreviews[ url ];
761
920
  }
762
921
 
@@ -767,12 +926,12 @@ export function getEmbedPreview( state, url ) {
767
926
  * We need to be able to determine if a URL is embeddable or not, based on what we
768
927
  * get back from the oEmbed preview API.
769
928
  *
770
- * @param {Object} state Data state.
771
- * @param {string} url Embedded URL.
929
+ * @param state Data state.
930
+ * @param url Embedded URL.
772
931
  *
773
- * @return {boolean} Is the preview for the URL an oEmbed link fallback.
932
+ * @return Is the preview for the URL an oEmbed link fallback.
774
933
  */
775
- export function isPreviewEmbedFallback( state, url ) {
934
+ export function isPreviewEmbedFallback( state: State, url: string ): boolean {
776
935
  const preview = state.embedPreviews[ url ];
777
936
  const oEmbedLinkCheck = '<a href="' + url + '">' + url + '</a>';
778
937
  if ( ! preview ) {
@@ -790,15 +949,20 @@ export function isPreviewEmbedFallback( state, url ) {
790
949
  *
791
950
  * https://developer.wordpress.org/rest-api/reference/
792
951
  *
793
- * @param {Object} state Data state.
794
- * @param {string} action Action to check. One of: 'create', 'read', 'update', 'delete'.
795
- * @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
796
- * @param {string=} id Optional ID of the rest resource to check.
952
+ * @param state Data state.
953
+ * @param action Action to check. One of: 'create', 'read', 'update', 'delete'.
954
+ * @param resource REST resource to check, e.g. 'media' or 'posts'.
955
+ * @param id Optional ID of the rest resource to check.
797
956
  *
798
- * @return {boolean|undefined} Whether or not the user can perform the action,
957
+ * @return Whether or not the user can perform the action,
799
958
  * or `undefined` if the OPTIONS request is still being made.
800
959
  */
801
- export function canUser( state, action, resource, id ) {
960
+ export function canUser(
961
+ state: State,
962
+ action: string,
963
+ resource: string,
964
+ id?: RecordKey
965
+ ): boolean | undefined {
802
966
  const key = compact( [ action, resource, id ] ).join( '/' );
803
967
  return get( state, [ 'userPermissions', key ] );
804
968
  }
@@ -811,14 +975,19 @@ export function canUser( state, action, resource, id ) {
811
975
  *
812
976
  * https://developer.wordpress.org/rest-api/reference/
813
977
  *
814
- * @param {Object} state Data state.
815
- * @param {string} kind Entity kind.
816
- * @param {string} name Entity name.
817
- * @param {string} recordId Record's id.
818
- * @return {boolean|undefined} Whether or not the user can edit,
978
+ * @param state Data state.
979
+ * @param kind Entity kind.
980
+ * @param name Entity name.
981
+ * @param recordId Record's id.
982
+ * @return Whether or not the user can edit,
819
983
  * or `undefined` if the OPTIONS request is still being made.
820
984
  */
821
- export function canUserEditEntityRecord( state, kind, name, recordId ) {
985
+ export function canUserEditEntityRecord(
986
+ state: State,
987
+ kind: string,
988
+ name: string,
989
+ recordId: RecordKey
990
+ ): boolean | undefined {
822
991
  const entityConfig = getEntityConfig( state, kind, name );
823
992
  if ( ! entityConfig ) {
824
993
  return false;
@@ -834,27 +1003,36 @@ export function canUserEditEntityRecord( state, kind, name, recordId ) {
834
1003
  * May return multiple autosaves since the backend stores one autosave per
835
1004
  * author for each post.
836
1005
  *
837
- * @param {Object} state State tree.
838
- * @param {string} postType The type of the parent post.
839
- * @param {number} postId The id of the parent post.
1006
+ * @param state State tree.
1007
+ * @param postType The type of the parent post.
1008
+ * @param postId The id of the parent post.
840
1009
  *
841
- * @return {?Array} An array of autosaves for the post, or undefined if there is none.
1010
+ * @return An array of autosaves for the post, or undefined if there is none.
842
1011
  */
843
- export function getAutosaves( state, postType, postId ) {
1012
+ export function getAutosaves(
1013
+ state: State,
1014
+ postType: string,
1015
+ postId: RecordKey
1016
+ ): Array< any > | undefined {
844
1017
  return state.autosaves[ postId ];
845
1018
  }
846
1019
 
847
1020
  /**
848
1021
  * Returns the autosave for the post and author.
849
1022
  *
850
- * @param {Object} state State tree.
851
- * @param {string} postType The type of the parent post.
852
- * @param {number} postId The id of the parent post.
853
- * @param {number} authorId The id of the author.
1023
+ * @param state State tree.
1024
+ * @param postType The type of the parent post.
1025
+ * @param postId The id of the parent post.
1026
+ * @param authorId The id of the author.
854
1027
  *
855
- * @return {?Object} The autosave for the post and author.
1028
+ * @return The autosave for the post and author.
856
1029
  */
857
- export function getAutosave( state, postType, postId, authorId ) {
1030
+ export function getAutosave(
1031
+ state: State,
1032
+ postType: string,
1033
+ postId: RecordKey,
1034
+ authorId: RecordKey
1035
+ ): EntityRecord | undefined {
858
1036
  if ( authorId === undefined ) {
859
1037
  return;
860
1038
  }
@@ -866,14 +1044,18 @@ export function getAutosave( state, postType, postId, authorId ) {
866
1044
  /**
867
1045
  * Returns true if the REST request for autosaves has completed.
868
1046
  *
869
- * @param {Object} state State tree.
870
- * @param {string} postType The type of the parent post.
871
- * @param {number} postId The id of the parent post.
1047
+ * @param state State tree.
1048
+ * @param postType The type of the parent post.
1049
+ * @param postId The id of the parent post.
872
1050
  *
873
- * @return {boolean} True if the REST request was completed. False otherwise.
1051
+ * @return True if the REST request was completed. False otherwise.
874
1052
  */
875
1053
  export const hasFetchedAutosaves = createRegistrySelector(
876
- ( select ) => ( state, postType, postId ) => {
1054
+ ( select ) => (
1055
+ state: State,
1056
+ postType: string,
1057
+ postId: RecordKey
1058
+ ): boolean => {
877
1059
  return select( STORE_NAME ).hasFinishedResolution( 'getAutosaves', [
878
1060
  postType,
879
1061
  postId,
@@ -895,13 +1077,14 @@ export const hasFetchedAutosaves = createRegistrySelector(
895
1077
  * );
896
1078
  * ```
897
1079
  *
898
- * @param {Object} state Editor state.
1080
+ * @param state Editor state.
899
1081
  *
900
- * @return {*} A value whose reference will change only when an edit occurs.
1082
+ * @return A value whose reference will change only when an edit occurs.
901
1083
  */
902
1084
  export const getReferenceByDistinctEdits = createSelector(
903
- () => [],
904
- ( state ) => [
1085
+ // This unused state argument is listed here for the documentation generating tool (docgen).
1086
+ ( state: State ) => [],
1087
+ ( state: State ) => [
905
1088
  state.undo.length,
906
1089
  state.undo.offset,
907
1090
  state.undo.flattenedUndo,
@@ -911,12 +1094,15 @@ export const getReferenceByDistinctEdits = createSelector(
911
1094
  /**
912
1095
  * Retrieve the frontend template used for a given link.
913
1096
  *
914
- * @param {Object} state Editor state.
915
- * @param {string} link Link.
1097
+ * @param state Editor state.
1098
+ * @param link Link.
916
1099
  *
917
- * @return {Object?} The template record.
1100
+ * @return The template record.
918
1101
  */
919
- export function __experimentalGetTemplateForLink( state, link ) {
1102
+ export function __experimentalGetTemplateForLink(
1103
+ state: State,
1104
+ link: string
1105
+ ): WpTemplate< 'edit' > | null {
920
1106
  const records = getEntityRecords( state, 'postType', 'wp_template', {
921
1107
  'find-template': link,
922
1108
  } );
@@ -936,11 +1122,13 @@ export function __experimentalGetTemplateForLink( state, link ) {
936
1122
  /**
937
1123
  * Retrieve the current theme's base global styles
938
1124
  *
939
- * @param {Object} state Editor state.
1125
+ * @param state Editor state.
940
1126
  *
941
- * @return {Object|null} The Global Styles object.
1127
+ * @return The Global Styles object.
942
1128
  */
943
- export function __experimentalGetCurrentThemeBaseGlobalStyles( state ) {
1129
+ export function __experimentalGetCurrentThemeBaseGlobalStyles(
1130
+ state: State
1131
+ ): any {
944
1132
  const currentTheme = getCurrentTheme( state );
945
1133
  if ( ! currentTheme ) {
946
1134
  return null;
@@ -951,11 +1139,13 @@ export function __experimentalGetCurrentThemeBaseGlobalStyles( state ) {
951
1139
  /**
952
1140
  * Return the ID of the current global styles object.
953
1141
  *
954
- * @param {Object} state Data state.
1142
+ * @param state Data state.
955
1143
  *
956
- * @return {string|null} The current global styles ID.
1144
+ * @return The current global styles ID.
957
1145
  */
958
- export function __experimentalGetCurrentThemeGlobalStylesVariations( state ) {
1146
+ export function __experimentalGetCurrentThemeGlobalStylesVariations(
1147
+ state: State
1148
+ ): string | null {
959
1149
  const currentTheme = getCurrentTheme( state );
960
1150
  if ( ! currentTheme ) {
961
1151
  return null;
@@ -966,21 +1156,21 @@ export function __experimentalGetCurrentThemeGlobalStylesVariations( state ) {
966
1156
  /**
967
1157
  * Retrieve the list of registered block patterns.
968
1158
  *
969
- * @param {Object} state Data state.
1159
+ * @param state Data state.
970
1160
  *
971
- * @return {Array} Block pattern list.
1161
+ * @return Block pattern list.
972
1162
  */
973
- export function getBlockPatterns( state ) {
1163
+ export function getBlockPatterns( state: State ): Array< any > {
974
1164
  return state.blockPatterns;
975
1165
  }
976
1166
 
977
1167
  /**
978
1168
  * Retrieve the list of registered block pattern categories.
979
1169
  *
980
- * @param {Object} state Data state.
1170
+ * @param state Data state.
981
1171
  *
982
- * @return {Array} Block pattern category list.
1172
+ * @return Block pattern category list.
983
1173
  */
984
- export function getBlockPatternCategories( state ) {
1174
+ export function getBlockPatternCategories( state: State ): Array< any > {
985
1175
  return state.blockPatternCategories;
986
1176
  }