From 5c363b435e371ffe29a55bd3c2dd861368262dba Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Thu, 6 Apr 2023 12:44:05 -0700 Subject: [PATCH 1/3] Convert WordPress data controls to typescript --- packages/data-controls/README.md | 12 +++-- .../data-controls/src/{index.js => index.ts} | 50 ++++++++++++++----- packages/data-controls/tsconfig.json | 14 ++++++ tsconfig.json | 1 + 4 files changed, 61 insertions(+), 16 deletions(-) rename packages/data-controls/src/{index.js => index.ts} (66%) create mode 100644 packages/data-controls/tsconfig.json diff --git a/packages/data-controls/README.md b/packages/data-controls/README.md index d8263d148c8d66..36f57689d85650 100644 --- a/packages/data-controls/README.md +++ b/packages/data-controls/README.md @@ -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`: +- _actionName_ `string`: +- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control. ### select @@ -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`: +- _selectorName_ `string`: +- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control. ### syncSelect @@ -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`: +- _selectorName_ `string`: +- _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control. diff --git a/packages/data-controls/src/index.js b/packages/data-controls/src/index.ts similarity index 66% rename from packages/data-controls/src/index.js rename to packages/data-controls/src/index.ts index 62671b41f139ff..38d69b4d70366a 100644 --- a/packages/data-controls/src/index.js +++ b/packages/data-controls/src/index.ts @@ -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. @@ -24,7 +26,7 @@ import deprecated from '@wordpress/deprecated'; * * @return {Object} The control descriptor. */ -export function apiFetch( request ) { +export function apiFetch( request: APIFetchOptions ) { return { type: 'API_FETCH', request, @@ -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 + * @param selectorName + * @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 ); + 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 + * @param selectorName + * @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 + * @param actionName + * @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 ); } /** @@ -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, @@ -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 ); }, }; diff --git a/packages/data-controls/tsconfig.json b/packages/data-controls/tsconfig.json new file mode 100644 index 00000000000000..965853e7c46b33 --- /dev/null +++ b/packages/data-controls/tsconfig.json @@ -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/**/*" ] +} diff --git a/tsconfig.json b/tsconfig.json index e62a55a68bc653..fcf85b204f6789 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" }, From e7a481de0406d780f88efa0c24a882061646cf7e Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Thu, 6 Apr 2023 12:48:33 -0700 Subject: [PATCH 2/3] Add types to package.json --- packages/data-controls/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/data-controls/package.json b/packages/data-controls/package.json index a0e6f878b88f37..aa12c7f8948bfa 100644 --- a/packages/data-controls/package.json +++ b/packages/data-controls/package.json @@ -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", From af509a6af03dd4960325d2c48a20ce961f38063a Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Fri, 7 Apr 2023 12:11:48 -0700 Subject: [PATCH 3/3] Update changelog and add more doc comments --- packages/data-controls/CHANGELOG.md | 4 ++++ packages/data-controls/README.md | 12 ++++++------ packages/data-controls/src/index.ts | 12 ++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/data-controls/CHANGELOG.md b/packages/data-controls/CHANGELOG.md index 8df211b0342ee0..fe96cbeb0e4ebb 100644 --- a/packages/data-controls/CHANGELOG.md +++ b/packages/data-controls/CHANGELOG.md @@ -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) diff --git a/packages/data-controls/README.md b/packages/data-controls/README.md index 36f57689d85650..d859cefbf0c9c0 100644 --- a/packages/data-controls/README.md +++ b/packages/data-controls/README.md @@ -83,8 +83,8 @@ Alias for the `dispatch` control in the `@wordpress/data` package. _Parameters_ -- _storeNameOrDescriptor_ `string | StoreDescriptor`: -- _actionName_ `string`: +- _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 @@ -94,8 +94,8 @@ Alias for the `resolveSelect` built-in control in the `@wordpress/data` package. _Parameters_ -- _storeNameOrDescriptor_ `string | StoreDescriptor`: -- _selectorName_ `string`: +- _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 @@ -105,8 +105,8 @@ Alias for the `select` built-in control in the `@wordpress/data` package. _Parameters_ -- _storeNameOrDescriptor_ `string | StoreDescriptor`: -- _selectorName_ `string`: +- _storeNameOrDescriptor_ `string | StoreDescriptor`: The store object or identifier. +- _selectorName_ `string`: The selector name. - _args_ `any[]`: Arguments passed without change to the `@wordpress/data` control. diff --git a/packages/data-controls/src/index.ts b/packages/data-controls/src/index.ts index 38d69b4d70366a..9edc6502ef881f 100644 --- a/packages/data-controls/src/index.ts +++ b/packages/data-controls/src/index.ts @@ -37,8 +37,8 @@ export function apiFetch( request: APIFetchOptions ) { * Control for resolving a selector in a registered data store. * Alias for the `resolveSelect` built-in control in the `@wordpress/data` package. * - * @param storeNameOrDescriptor - * @param selectorName + * @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( @@ -62,8 +62,8 @@ export function select( * Control for calling a selector in a registered data store. * Alias for the `select` built-in control in the `@wordpress/data` package. * - * @param storeNameOrDescriptor - * @param selectorName + * @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( @@ -83,8 +83,8 @@ export function syncSelect( * Control for dispatching an action in a registered data store. * Alias for the `dispatch` control in the `@wordpress/data` package. * - * @param storeNameOrDescriptor - * @param actionName + * @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(