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

TypeScript: Fix and improve types for private-apis #66667

Merged
merged 3 commits into from
Nov 1, 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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* wordpress/private-apis – the utilities to enable private cross-package
* exports of private APIs.
*
* This "implementation.js" file is needed for the sake of the unit tests. It
* This "implementation.ts" file is needed for the sake of the unit tests. It
* exports more than the public API of the package to aid in testing.
*/

Expand Down Expand Up @@ -36,10 +36,8 @@ const CORE_MODULES_USING_PRIVATE_APIS = [
/**
* A list of core modules that already opted-in to
* the privateApis package.
*
* @type {string[]}
*/
const registeredPrivateApis = [];
const registeredPrivateApis: Array< string > = [];

/*
* Warning for theme and plugin developers.
Expand All @@ -65,13 +63,13 @@ const allowReRegistration = globalThis.IS_WORDPRESS_CORE ? false : true;
* Called by a @wordpress package wishing to opt-in to accessing or exposing
* private private APIs.
*
* @param {string} consent The consent string.
* @param {string} moduleName The name of the module that is opting in.
* @return {{lock: typeof lock, unlock: typeof unlock}} An object containing the lock and unlock functions.
* @param consent The consent string.
* @param moduleName The name of the module that is opting in.
* @return An object containing the lock and unlock functions.
*/
export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
consent,
moduleName
consent: string,
moduleName: string
) => {
if ( ! CORE_MODULES_USING_PRIVATE_APIS.includes( moduleName ) ) {
throw new Error(
Expand Down Expand Up @@ -135,10 +133,10 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
* // { a: 1 }
* ```
*
* @param {any} object The object to bind the private data to.
* @param {any} privateData The private data to bind to the object.
* @param object The object to bind the private data to.
* @param privateData The private data to bind to the object.
*/
function lock( object, privateData ) {
function lock( object: Record< symbol, WeakKey >, privateData: unknown ) {
if ( ! object ) {
throw new Error( 'Cannot lock an undefined object.' );
}
Expand Down Expand Up @@ -168,10 +166,10 @@ function lock( object, privateData ) {
* // { a: 1 }
* ```
*
* @param {any} object The object to unlock the private data from.
* @return {any} The private data bound to the object.
* @param object The object to unlock the private data from.
* @return The private data bound to the object.
*/
function unlock( object ) {
function unlock( object: Record< symbol, WeakKey > ) {
Copy link
Member

Choose a reason for hiding this comment

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

A little better type would be unlock<T>(object: unknown): T because:

  • the function can safely work with any object and try to unlock it. We might run into problems with the Record type if the locked object doesn't have a compatible type. That will probably happen a lot as we migrate the poorly-typed-yet codebase.
  • we should be able to cast the return value to be an object of known type with private methods.

I predict that we'll run into these issues as we use the typed lock/unlock and will eventually come back to revisit this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated in #66682

Copy link
Member Author

Choose a reason for hiding this comment

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

That signature seems to break a lot of existing poorly typed usage of lock and unlock

if ( ! object ) {
throw new Error( 'Cannot unlock an undefined object.' );
}
Expand All @@ -198,9 +196,9 @@ const __private = Symbol( 'Private API ID' );
* Private function to allow the unit tests to allow
* a mock module to access the private APIs.
*
* @param {string} name The name of the module.
* @param name The name of the module.
*/
export function allowCoreModule( name ) {
export function allowCoreModule( name: string ) {
CORE_MODULES_USING_PRIVATE_APIS.push( name );
}

Expand Down
File renamed without changes.
Loading