Skip to content

Add some typing to block-editor utils#76885

Open
wwahammy wants to merge 3 commits intoWordPress:trunkfrom
wwahammy:ts-block-editor
Open

Add some typing to block-editor utils#76885
wwahammy wants to merge 3 commits intoWordPress:trunkfrom
wwahammy:ts-block-editor

Conversation

@wwahammy
Copy link
Copy Markdown
Contributor

@wwahammy wwahammy commented Mar 29, 2026

What?

This continues the effort of typing more of the @wordpress/block-editor package. I worked on two particular files in the src/utils subfolder: object.js and pasting.js

Why?

Potentially improve code quality with more typing in @wordpress/block-editor. Additionally the tsconfig.json for the package says the package is being slowly typed so I figured I'd help 😄

How?

I noticed that the src/utils/object.js file is mostly the same as similar utils file in the global-styles-engine. Given that I figured I'd migrate the file straight to ts so I can reuse the code.

Typing the src/utils/pasting.js file required two small changes to the getPasteEventData function:

  1. I needed to set the type of the param to a ClipboardEvent.
  2. I needed to add a guard statement for ClipboardEvent is null. If you read the code, the try block will technically catch that situation but TS doesn't realize that. I don't love the use of a guard statement so if there's a better option, I'm for it but it does work. Additionally, pasting is not that common of an event so a single if statement isn't going to have much if any effect on performance.

Testing Instructions

Likely none required, only one functional change, a redundant null check.

Use of AI Tools

None.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Mar 29, 2026
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I found copies of the first two functions in the global-styles-engine so I figured I'd just switch this to typescript like that file and migrate the contents over.

let plainText = '';
let html = '';

if ( ! clipboardData ) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is slightly redundant as the try block would catch when clipboardData is null. Unfortunately, Typescript doesn't realize that so it doesn't know that clipboardData is non-null in the rest of the method. This works around that limitation.

@wwahammy wwahammy marked this pull request as ready for review March 29, 2026 03:17
@wwahammy wwahammy requested a review from ellatrix as a code owner March 29, 2026 03:17
@github-actions
Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: [Package] Block editor.

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: wwahammy <wwahammy@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant