From 423976acf4633a86ef08d6cf5a367485387a5a06 Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Tue, 11 Apr 2023 21:38:17 +0200 Subject: [PATCH] Convert wp data controls to typescript (#49647) * Convert WordPress data controls to typescript --- packages/data-controls/CHANGELOG.md | 4 ++ packages/data-controls/README.md | 12 +++-- packages/data-controls/package.json | 1 + .../data-controls/src/{index.js => index.ts} | 50 ++++++++++++++----- packages/data-controls/tsconfig.json | 14 ++++++ tsconfig.json | 1 + 6 files changed, 66 insertions(+), 16 deletions(-) rename packages/data-controls/src/{index.js => index.ts} (64%) create mode 100644 packages/data-controls/tsconfig.json diff --git a/packages/data-controls/CHANGELOG.md b/packages/data-controls/CHANGELOG.md index 8df211b0342ee..fe96cbeb0e4eb 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 d107c4118c835..fdcc66fc403ea 100644 --- a/packages/data-controls/README.md +++ b/packages/data-controls/README.md @@ -81,7 +81,9 @@ Control for dispatching an action in a registered data store. Alias for the `dis _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 @@ -89,7 +91,9 @@ Control for resolving a selector in a registered data store. Alias for the `reso _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 @@ -97,7 +101,9 @@ Control for calling a selector in a registered data store. Alias for the `select _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. diff --git a/packages/data-controls/package.json b/packages/data-controls/package.json index a0e6f878b88f3..aa12c7f8948bf 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", diff --git a/packages/data-controls/src/index.js b/packages/data-controls/src/index.ts similarity index 64% rename from packages/data-controls/src/index.js rename to packages/data-controls/src/index.ts index 62671b41f139f..9edc6502ef881 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 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 ); + 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 ); } /** @@ -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 0000000000000..965853e7c46b3 --- /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 e62a55a68bc65..fcf85b204f678 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" },