Registry / writing / wp-types

wp-types

JSON →
library4.70.1jsnpmunverified

TypeScript definitions for WordPress PHP objects and REST API responses (v4.70.1). Provides well-documented interfaces for WP_Post, WP_Term, WP_User, WP_Comment, WP_Error, WP_Query, WP_Block, and 30+ REST API endpoints including blocks, menus, templates, global styles, fonts, and media. Updated for WordPress 7.0. Released monthly via npm with semantic versioning. Differentiator: comprehensive coverage of WordPress core data structures and REST API schemas, auto-generated from WordPress source, with TypeScript 5.x support.

npm install wp-types
INSTALL
IMPORT
SIG · WP-TYPES
W
wp-types
writingjavascriptv4.70.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

WP_Post
import { WP_Post } from 'wp-types'
import WP_Post from 'wp-types'
Named export, not default. Requires TypeScript 4.0+ with esModuleInterop.
WP_REST_API_Post
import { WP_REST_API_Post } from 'wp-types'
import { WP_REST_API_Post } from 'wp-types/lib/post'
All types are exported from the package root; no subpath exports exist.
WP_User
import { WP_User } from 'wp-types'
const WP_User = require('wp-types').WP_User
Both CJS require and ESM import work, but named import is preferred.
WP_Error
import type { WP_Error } from 'wp-types'
import { WP_Error } from 'wp-types'
WP_Error is a type-only interface; use `import type` when not using at runtime.
WP_REST_API_Blocks
import { WP_REST_API_Blocks } from 'wp-types'
import { WP_REST_API_Block } from 'wp-types'
Array types have plural names, singular types are for single items.

Imports and sample usage of WP_Post, WP_REST_API_Post, and WP_User types demonstrating PHP object and REST API response shapes.

import type { WP_Post, WP_REST_API_Post, WP_User } from 'wp-types'; const post: WP_Post = { ID: 1, post_author: '1', post_date: '2024-01-01 00:00:00', post_date_gmt: '2024-01-01 00:00:00', post_content: 'Hello World', post_title: 'Hello', post_excerpt: '', post_status: 'publish', comment_status: 'open', ping_status: 'open', post_password: '', post_name: 'hello', to_ping: '', pinged: '', post_modified: '2024-01-01 00:00:00', post_modified_gmt: '2024-01-01 00:00:00', post_content_filtered: '', post_parent: 0, guid: 'https://example.com/?p=1', menu_order: 0, post_type: 'post', post_mime_type: '', comment_count: '0', filter: 'raw', }; const apiPost: WP_REST_API_Post = { id: 1, date: '2024-01-01T00:00:00', date_gmt: '2024-01-01T00:00:00', guid: { rendered: 'https://example.com/?p=1' }, modified: '2024-01-01T00:00:00', modified_gmt: '2024-01-01T00:00:00', slug: 'hello', status: 'publish', type: 'post', link: 'https://example.com/hello', title: { rendered: 'Hello' }, content: { rendered: '<p>Hello World</p>', protected: false }, excerpt: { rendered: '', protected: false }, author: 1, featured_media: 0, comment_status: 'open', ping_status: 'open', sticky: false, template: '', format: 'standard', meta: {}, categories: [1], tags: [], _links: { self: [{ href: 'https://example.com/wp-json/wp/v2/posts/1' }], collection: [{ href: 'https://example.com/wp-json/wp/v2/posts' }], about: [{ href: 'https://example.com/wp-json/wp/v2/types/post' }], author: [{ href: 'https://example.com/wp-json/wp/v2/users/1' }], replies: [{ href: 'https://example.com/wp-json/wp/v2/comments?post=1' }], 'version-history': [{ href: 'https://example.com/wp-json/wp/v2/posts/1/revisions', count: 0 }], 'wp:attachment': [{ href: 'https://example.com/wp-json/wp/v2/media?parent=1' }], 'wp:term': [{ href: 'https://example.com/wp-json/wp/v2/categories?post=1' }, { href: 'https://example.com/wp-json/wp/v2/tags?post=1' }], curies: [{ name: 'wp', href: 'https://api.w.org/{rel}', templated: true }], }, }; const user: WP_User = { ID: 1, user_login: 'admin', user_pass: '', user_nicename: 'admin', user_email: 'admin@example.com', user_url: '', user_registered: '2024-01-01 00:00:00', user_activation_key: '', user_status: 0, display_name: 'Admin', }; console.log(post, apiPost, user);
Debug
Known issues
breakingv4.0.0 changed export structure: all types are now named exports from root package. Previously types were at subpaths like 'wp-types/lib/post'.
fix
Update imports to 'import { WP_Post } from 'wp-types'' instead of 'import { WP_Post } from 'wp-types/lib/post'.
affects: >=4.0.0
breakingv3.0.0 removed 'WP_Error' as a runtime value; it is now a pure type. Instantiating it at runtime will break.
fix
Use `import type { WP_Error } from 'wp-types'` and handle errors via other means.
affects: >=3.0.0
deprecatedv2.x deprecated commonjs require() style usage; future versions may drop CJS support.
fix
Use ESM imports (import { ... } from 'wp-types') instead of require().
affects: >=2.0.0
gotchaREST API response types expect the '_links' property to be present and structured exactly as WordPress returns. Missing '_links' will cause type errors.
fix
Ensure your REST API responses include a properly typed '_links' object with the correct 'rel' keys.
affects: >=1.0.0
gotchaPHP object types (e.g., WP_Post) use string fields for IDs and counts (e.g., ID is number, but post_author is string). Do not assume numeric types.
fix
Cast values appropriately with Number() or String() when needed.
affects: >=1.0.0
Errors
Common errors & fixes
Type error: Property 'post_author' is missing in type '{ ... }' but required in type 'WP_Post'
Missing required property in object literal assigned to WP_Post type.
fix
Add all required properties as defined in WP_Post interface, including 'post_author', 'post_date', etc.
Module '"wp-types"' has no exported member 'WP_REST_API_Post'. Did you mean to use 'import WP_REST_API_Post from "wp-types"' instead?
Using default import for a named export or missing the type from the package.
fix
Use named import: `import { WP_REST_API_Post } from 'wp-types'`.
Cannot find module 'wp-types' or its corresponding type declarations.
Package not installed or type resolution misconfigured.
fix
Run `npm install wp-types --save-dev` and ensure tsconfig.json includes 'node_modules/@types' or 'types' field.
Type 'string' is not assignable to type 'number' in property 'ID'
Mixing up PHP object (ID is number) and REST API (id is number) types, but assigning string to ID.
fix
Ensure ID is a number, e.g., `ID: 1` not `ID: '1'`.
Upgrade
Version history
4.70.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
44 hits · last 30 days
node
40
Resources
wp-types — npm install wp-types · libregistry