Skip to content

Commit

Permalink
Simplify the TS-based lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Feb 28, 2022
1 parent c22383e commit b9eb573
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 423 deletions.
457 changes: 304 additions & 153 deletions packages/core-data/src/entities.ts

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/core-data/src/types/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
PingStatus,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
declare module './base-entity-records' {
export namespace BaseEntityRecords {
export interface Attachment< C extends Context > {
/**
* The date the post was published, in the site's timezone.
Expand Down Expand Up @@ -142,5 +142,5 @@ declare module './base-entity-types' {
}

export type Attachment< C extends Context > = OmitNevers<
_BaseEntityTypes.Attachment< C >
_BaseEntityRecords.Attachment< C >
>;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* This module exists solely to make the BaseEntityTypes namespace extensible
* This module exists solely to make the BaseEntityRecords namespace extensible
* with declaration merging:
*
* ```ts
* declare module './base-entity-types' {
* export namespace BaseEntityTypes {
* declare module './base-entity-records' {
* export namespace BaseEntityRecords {
* export interface Comment< C extends Context > {
* id: number;
* // ...
Expand All @@ -19,7 +19,7 @@
* ```ts
* import type { Context } from '@wordpress/core-data';
* declare module '@wordpress/core-data' {
* export namespace BaseEntityTypes {
* export namespace BaseEntityRecords {
* export interface Comment< C extends Context > {
* numberOfViews: number;
* }
Expand All @@ -33,4 +33,4 @@
* // c.id is still present
* ```
*/
export namespace BaseEntityTypes {}
export namespace BaseEntityRecords {}
8 changes: 4 additions & 4 deletions packages/core-data/src/types/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
OmitNevers,
RenderedText,
} from './helpers';
import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';

export type CommentStatus = 'hold' | 'approve' | 'spam' | 'trash' | '1' | '0';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
declare module './base-entity-records' {
export namespace BaseEntityRecords {
export interface Comment< C extends Context > {
/**
* Unique identifier for the comment.
Expand Down Expand Up @@ -92,5 +92,5 @@ declare module './base-entity-types' {
}

export type Comment< C extends Context > = OmitNevers<
_BaseEntityTypes.Comment< C >
_BaseEntityRecords.Comment< C >
>;
164 changes: 0 additions & 164 deletions packages/core-data/src/types/configure.ts

This file was deleted.

33 changes: 31 additions & 2 deletions packages/core-data/src/types/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { CoreEntityRecord } from './index';
import { EntityRecord } from './index';

export interface AvatarUrls {
/**
Expand Down Expand Up @@ -148,6 +148,35 @@ export interface RenderedText< C extends Context > {
* // updatablePost.title is a string
* ```
*/
export type Updatable< T extends CoreEntityRecord< 'edit' > > = {
export type Updatable< T extends EntityRecord< 'edit' > > = {
[ K in keyof T ]: T[ K ] extends RenderedText< any > ? string : T[ K ];
};

/**
* TODO Docstring
*/
export type EntityFromConfig<
E extends {
kind: string;
name: string;
key?: string;
baseURLParams?: EntityQuery< any >;
},
R
> = {
kind: E[ 'kind' ];
name: E[ 'name' ];
recordType: R;
key: E[ 'key' ] extends string ? E[ 'key' ] : 'id';
defaultContext: E[ 'baseURLParams' ] extends EntityQuery< infer C >
? C
: 'view';
};

/**
* TODO Docstring
*/
export type EntityQuery< C extends Context > = {
[ key: string ]: string;
context?: C;
};
Loading

0 comments on commit b9eb573

Please sign in to comment.