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

Convert wp data controls to typescript #49647

Merged
merged 3 commits into from
Apr 11, 2023
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
4 changes: 4 additions & 0 deletions packages/data-controls/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking Changes

- Publish Typescript build types to npm. ([#49647](https://github.com/WordPress/gutenberg/pull/49647))

## 2.30.0 (2023-03-29)

## 2.29.0 (2023-03-15)
Expand Down
12 changes: 9 additions & 3 deletions packages/data-controls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ Alias for the `dispatch` control in the `@wordpress/data` package.

_Parameters_

- _args_ `Array`: Arguments passed without change to the `@wordpress/data` control.
- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier.
- _actionName_ `string`: The action name.
- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control.

### select

Expand All @@ -92,7 +94,9 @@ Alias for the `resolveSelect` built-in control in the `@wordpress/data` package.

_Parameters_

- _args_ `Array`: Arguments passed without change to the `@wordpress/data` control.
- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier.
- _selectorName_ `string`: The selector name.
- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control.

### syncSelect

Expand All @@ -101,7 +105,9 @@ Alias for the `select` built-in control in the `@wordpress/data` package.

_Parameters_

- _args_ `Array`: Arguments passed without change to the `@wordpress/data` control.
- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier.
- _selectorName_ `string`: The selector name.
- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control.

<!-- END TOKEN(Autogenerated API docs) -->

Expand Down
1 change: 1 addition & 0 deletions packages/data-controls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/api-fetch": "file:../api-fetch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import triggerFetch from '@wordpress/api-fetch';
import { controls as dataControls } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import type { StoreDescriptor } from '@wordpress/data';
import type { APIFetchOptions } from '@wordpress/api-fetch';

/**
* Dispatches a control action for triggering an api fetch call.
Expand All @@ -24,7 +26,7 @@ import deprecated from '@wordpress/deprecated';
*
* @return {Object} The control descriptor.
*/
export function apiFetch( request ) {
export function apiFetch( request: APIFetchOptions ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

APIFetchOptions is what DT uses for this

return {
type: 'API_FETCH',
request,
Expand All @@ -35,45 +37,67 @@ export function apiFetch( request ) {
* Control for resolving a selector in a registered data store.
* Alias for the `resolveSelect` built-in control in the `@wordpress/data` package.
*
* @param {Array} args Arguments passed without change to the `@wordpress/data` control.
* @param storeNameOrDescriptor The store object or identifier.
* @param selectorName The selector name.
* @param args Arguments passed without change to the `@wordpress/data` control.
*/
export function select( ...args ) {
export function select(
storeNameOrDescriptor: string | StoreDescriptor,
selectorName: string,
...args: any[]
) {
deprecated( '`select` control in `@wordpress/data-controls`', {
since: '5.7',
alternative: 'built-in `resolveSelect` control in `@wordpress/data`',
} );

return dataControls.resolveSelect( ...args );
Copy link
Member Author

Choose a reason for hiding this comment

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

Typescript wasn't happy about this because it's not specific enough.

Copy link
Member

Choose a reason for hiding this comment

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

I like it, it's way more predictable that way.

return dataControls.resolveSelect(
storeNameOrDescriptor,
selectorName,
...args
);
}

/**
* Control for calling a selector in a registered data store.
* Alias for the `select` built-in control in the `@wordpress/data` package.
*
* @param {Array} args Arguments passed without change to the `@wordpress/data` control.
* @param storeNameOrDescriptor The store object or identifier.
* @param selectorName The selector name.
* @param args Arguments passed without change to the `@wordpress/data` control.
*/
export function syncSelect( ...args ) {
export function syncSelect(
storeNameOrDescriptor: string | StoreDescriptor,
selectorName: string,
...args: any[]
) {
deprecated( '`syncSelect` control in `@wordpress/data-controls`', {
since: '5.7',
alternative: 'built-in `select` control in `@wordpress/data`',
} );

return dataControls.select( ...args );
return dataControls.select( storeNameOrDescriptor, selectorName, ...args );
}

/**
* Control for dispatching an action in a registered data store.
* Alias for the `dispatch` control in the `@wordpress/data` package.
*
* @param {Array} args Arguments passed without change to the `@wordpress/data` control.
* @param storeNameOrDescriptor The store object or identifier.
* @param actionName The action name.
* @param args Arguments passed without change to the `@wordpress/data` control.
*/
export function dispatch( ...args ) {
export function dispatch(
storeNameOrDescriptor: string | StoreDescriptor,
actionName: string,
...args: any[]
) {
deprecated( '`dispatch` control in `@wordpress/data-controls`', {
since: '5.7',
alternative: 'built-in `dispatch` control in `@wordpress/data`',
} );

return dataControls.dispatch( ...args );
return dataControls.dispatch( storeNameOrDescriptor, actionName, ...args );
}

/**
Expand All @@ -95,7 +119,7 @@ export function dispatch( ...args ) {
*
* @return {Object} The control descriptor.
*/
export const __unstableAwaitPromise = function ( promise ) {
export const __unstableAwaitPromise = function < T >( promise: Promise< T > ) {
return {
type: 'AWAIT_PROMISE',
promise,
Expand Down Expand Up @@ -130,8 +154,8 @@ export const __unstableAwaitPromise = function ( promise ) {
* store.
*/
export const controls = {
AWAIT_PROMISE: ( { promise } ) => promise,
API_FETCH( { request } ) {
AWAIT_PROMISE: < T >( { promise }: { promise: Promise< T > } ) => promise,
API_FETCH( { request }: { request: APIFetchOptions } ) {
return triggerFetch( request );
},
};
14 changes: 14 additions & 0 deletions packages/data-controls/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"types": [ "gutenberg-env" ]
},
"references": [
{ "path": "../api-fetch" },
{ "path": "../data" },
{ "path": "../deprecated" }
],
"include": [ "src/**/*" ]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{ "path": "packages/compose" },
{ "path": "packages/core-data" },
{ "path": "packages/data" },
{ "path": "packages/data-controls" },
{ "path": "packages/date" },
{ "path": "packages/deprecated" },
{ "path": "packages/docgen" },
Expand Down