Skip to content

Commit

Permalink
Improve "expected error" types (#207)
Browse files Browse the repository at this point in the history
* Commit registerBlockBindingsSource types to expected-errors

* Remove redundant export statements

* Move NullableKeys to type utils

---------

Co-authored-by: Max Schmeling <max.schmeling@automattic.com>
  • Loading branch information
chriszarate and maxschmeling authored Nov 20, 2024
1 parent cb20d12 commit 07b2146
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/blocks/remote-data-container/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-expect-error -- Temporary registerBlockBindingsSource type error workaround for WordPress 6.7
import { registerBlockType, registerBlockBindingsSource } from '@wordpress/blocks';
import { registerBlockType } from '@wordpress/blocks';
import { addFilter } from '@wordpress/hooks';
import { registerFormatType } from '@wordpress/rich-text';

Expand All @@ -8,6 +7,7 @@ import { FieldShortcodeButton } from '@/blocks/remote-data-container/components/
import { Edit } from '@/blocks/remote-data-container/edit';
import { withBlockBindingShim } from '@/blocks/remote-data-container/filters/withBlockBinding';
import { Save } from '@/blocks/remote-data-container/save';
import { registerBlockBindingsSource } from '@/types/expected-errors/registerBlockBindingsSource';
import { getBlocksConfig } from '@/utils/localized-block-data';
import './style.scss';

Expand Down Expand Up @@ -47,13 +47,11 @@ addFilter(
5 // Ensure this runs before core filters
);

// eslint-disable-next-line -- Temporary registerBlockBindingsSource type error workaround for WordPress 6.7
registerBlockBindingsSource( {
name: 'remote-data/binding',
label: 'Remote Data Binding',
usesContext: [ 'remote-data-blocks/remoteData' ],
// eslint-disable-next-line -- Temporary registerBlockBindingsSource type error workaround for WordPress 6.7
getValues( { select, clientId, context, bindings }: any ) {
getValues() {
return {};
},
} );
36 changes: 36 additions & 0 deletions src/types/expected-errors/registerBlockBindingsSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @ts-expect-error Temporary registerBlockBindingsSource type error workaround for WordPress 6.7
import { registerBlockBindingsSource as originalRegisterBlockBindingsSource } from '@wordpress/blocks';

import type {
BlockEditorStoreActions,
BlockEditorStoreSelectors,
BlockEditorStoreDescriptor,
} from '@wordpress/block-editor';

interface GetValuesPayload< Context, Values > {
bindings: Values;
clientId: string;
context: Context;
select: ( store: BlockEditorStoreDescriptor ) => BlockEditorStoreSelectors;
}

interface SetValuesPayload< Context, Values > extends GetValuesPayload< Context, Values > {
dispatch: ( store: BlockEditorStoreDescriptor ) => BlockEditorStoreActions;
values: Values;
}

export interface BlockBindingsSource< Context = Record< string, unknown >, Values = unknown > {
canUserEditValue?: ( payload: GetValuesPayload< Context, Values > ) => boolean;
getValues?: ( payload: GetValuesPayload< Context, Values > ) => Values;
label?: string;
name: string;
setValues?: ( payload: SetValuesPayload< Context, Values > ) => void;
usesContext?: string[];
}

export function registerBlockBindingsSource< Context, Values >(
source: BlockBindingsSource< Context, Values >
): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
originalRegisterBlockBindingsSource( source );
}
4 changes: 0 additions & 4 deletions types/localized-settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ interface LocalizedSettingsData {
hash: string;
version: string;
}

type NullableKeys< T, K extends keyof T > = Omit< T, K > & {
[ P in K ]: T[ P ] | null;
};
3 changes: 3 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type NullableKeys< T, K extends keyof T > = Omit< T, K > & {
[ P in K ]: T[ P ] | null;
};
6 changes: 3 additions & 3 deletions types/wordpress__notices/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare module '@wordpress/notices' {
__unstableHTML?: boolean;
}

export interface WPNotice {
interface WPNotice {
id: string;
status: string;
content: string;
Expand All @@ -46,13 +46,13 @@ declare module '@wordpress/notices' {
id: string;
}

export interface NoticeStoreActions {
interface NoticeStoreActions {
createSuccessNotice: ( content: string, options?: CreateNoticeOptions ) => CreateNoticeReturn;
createErrorNotice: ( content: string, options?: CreateNoticeOptions ) => CreateNoticeReturn;
removeNotice: ( id: string, context?: string ) => RemoveNoticeReturn;
}

export interface NoticeStoreSelectors {
interface NoticeStoreSelectors {
getNotices: ( state: object = {}, context?: string ) => WPNotice[];
}
}

0 comments on commit 07b2146

Please sign in to comment.