Repository navigation
TypeScript: Migrate packages/list-reusable-blocks package to TypeScript
#70518
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
Changes from 2 commits
5dde8f8
bf1a575
638306b
d88ee65
2f33a44
0df42b3
9d45541
3d32343
d29e195
e061f7c
a9ec59d
3f6a89e
ea3fe78
f8b19ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,21 +7,29 @@ import apiFetch from '@wordpress/api-fetch'; | |||||
| * Internal dependencies | ||||||
| */ | ||||||
| import { readTextFile } from './file'; | ||||||
| import type { | ||||||
| ParsedContent, | ||||||
| PostType, | ||||||
| ReusableBlock, | ||||||
| ReusableBlockData, | ||||||
| } from './types'; | ||||||
|
|
||||||
| /** | ||||||
| * Import a reusable block from a JSON file. | ||||||
| * | ||||||
| * @param {File} file File. | ||||||
| * @return {Promise} Promise returning the imported reusable block. | ||||||
| * @param file - File to import | ||||||
| * @return Promise returning the imported reusable block | ||||||
| */ | ||||||
| async function importReusableBlock( file ) { | ||||||
| async function importReusableBlock( file: File ): Promise< ReusableBlock > { | ||||||
| const fileContent = await readTextFile( file ); | ||||||
| let parsedContent; | ||||||
| let parsedContent: ParsedContent; | ||||||
|
|
||||||
| try { | ||||||
| parsedContent = JSON.parse( fileContent ); | ||||||
| parsedContent = JSON.parse( fileContent ) as ParsedContent; | ||||||
|
Infinite-Null marked this conversation as resolved.
Resolved
|
||||||
| } catch ( e ) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| throw new Error( 'Invalid JSON file' ); | ||||||
| } | ||||||
|
|
||||||
| if ( | ||||||
| parsedContent.__file !== 'wp_block' || | ||||||
| ! parsedContent.title || | ||||||
|
|
@@ -33,8 +41,12 @@ async function importReusableBlock( file ) { | |||||
| ) { | ||||||
| throw new Error( 'Invalid pattern JSON file' ); | ||||||
| } | ||||||
| const postType = await apiFetch( { path: `/wp/v2/types/wp_block` } ); | ||||||
| const reusableBlock = await apiFetch( { | ||||||
|
|
||||||
| const postType = await apiFetch< PostType >( { | ||||||
| path: `/wp/v2/types/wp_block`, | ||||||
| } ); | ||||||
|
|
||||||
| const reusableBlock = await apiFetch< ReusableBlock >( { | ||||||
| path: `/wp/v2/${ postType.rest_base }`, | ||||||
| data: { | ||||||
| title: parsedContent.title, | ||||||
|
|
@@ -44,7 +56,7 @@ async function importReusableBlock( file ) { | |||||
| parsedContent.syncStatus === 'unsynced' | ||||||
| ? { wp_pattern_sync_status: parsedContent.syncStatus } | ||||||
| : undefined, | ||||||
| }, | ||||||
| } as ReusableBlockData, | ||||||
|
Infinite-Null marked this conversation as resolved.
Resolved
|
||||||
| method: 'POST', | ||||||
| } ); | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| interface ImportDropdownProps { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onUpload: ( data: any ) => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| interface ImportFormProps { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| instanceId: string | number; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onUpload: ( reusableBlock: any ) => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| onUpload: ( data: any ) => void; | |
| } | |
| interface ImportFormProps { | |
| instanceId: string | number; | |
| onUpload: ( reusableBlock: any ) => void; | |
| onUpload: ( data: ReusableBlockData ) => void; | |
| } | |
| interface ImportFormProps { | |
| instanceId: string | number; | |
| onUpload: ( reusableBlock: ReusableBlockData ) => void; |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type definitions use any type which defeats the purpose of TypeScript migration. The onUpload callback should have a properly typed parameter instead of any.
| onUpload: ( data: any ) => void; | |
| } | |
| interface ImportFormProps { | |
| instanceId: string | number; | |
| onUpload: ( reusableBlock: any ) => void; | |
| onUpload: ( data: ReusableBlockData ) => void; | |
| } | |
| interface ImportFormProps { | |
| instanceId: string | number; | |
| onUpload: ( reusableBlock: ReusableBlockData ) => void; |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using index signatures [ key: string ]: any weakens type safety. Consider defining specific properties or using a more restrictive approach if these extra properties are truly needed.
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using index signatures [ key: string ]: any weakens type safety. Consider defining specific properties or using a more restrictive approach if these extra properties are truly needed.
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using index signatures [ key: string ]: any weakens type safety. Consider defining specific properties or using a more restrictive approach if these extra properties are truly needed.
| [ key: string ]: any; | |
| [ key: string ]: unknown; |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using index signatures [ key: string ]: any weakens type safety. Consider defining specific properties or using a more restrictive approach if these extra properties are truly needed.
| [ key: string ]: any; | |
| [ key: string ]: unknown; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "references": [ | ||
| { "path": "../i18n" }, | ||
| { "path": "../compose" }, | ||
| { "path": "../components" }, | ||
| { "path": "../api-fetch" }, | ||
| { "path": "../blob" }, | ||
| { "path": "../element" } | ||
|
Infinite-Null marked this conversation as resolved.
Resolved
|
||
| ] | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.