Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve "expected error" types #207

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably isn't right, but not sure what it should be. we can improve these once we start to use them

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[];
}
}