Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.

Introduction

TypeScript provides strong typing that helps catch errors at compile time and improves developer experience with better autocompletion. CoCart provides OpenAPI specification files that can be used to automatically generate TypeScript types for all API endpoints, request bodies, and response schemas. This tutorial will show you how to generate and use TypeScript types from the CoCart OpenAPI specifications in your TypeScript projects.

Why Use Generated Types?

  • Type safety - Catch errors at compile time instead of runtime
  • Better IDE support - Get accurate autocompletion and IntelliSense
  • Automatic updates - Regenerate types when the API changes
  • Self-documenting - Types serve as inline documentation
  • Reduced errors - Prevent typos and incorrect API usage

Prerequisites

  • Node.js 18 or higher
  • A TypeScript project (Next.js, React, Node.js, etc.)
  • Basic knowledge of TypeScript
  • CoCart API endpoint URL

Available OpenAPI Specifications

CoCart provides several OpenAPI specification files depending on which API version you’re using:
This tutorial focuses on the v2 Stable API, which is recommended for all new projects.

Installation

Install the openapi-typescript package as a development dependency:
Or if you prefer Yarn:

Generating Types from Remote URL

The easiest way to generate types is directly from the GitHub repository:
This command:
  • Fetches the OpenAPI spec from GitHub
  • Generates TypeScript types
  • Saves them to src/types/cocart.ts
Add this command to your package.json scripts for easy regeneration:
Then run: npm run generate-types

Generating Types from Local File

If you have the OpenAPI specification file locally:
  1. Download the specification file to your project:
  1. Generate types from the local file:

Using Generated Types

Once you’ve generated the types, you can use them in your TypeScript code. The generated types follow this structure:

Example: Creating a Type-Safe API Client

Create a type-safe API client using the generated types:

Example: Using Types in React Components

Use the generated types in your React components:

Example: Using with Axios

Create a type-safe Axios client:

Example: Using with React Query

Combine generated types with React Query for powerful data fetching:

Advanced Configuration

Custom Output Options

The openapi-typescript CLI supports various options:
Available options:
  • --alphabetize - Sort types alphabetically
  • --export-type - Use export type instead of export interface
  • --path-params-as-types - Generate path parameters as types
  • --additional-properties - Allow additional properties in objects

Generating Multiple API Versions

If you need types for multiple API versions:

Using with Your Own WordPress Instance

If you’re using a custom CoCart installation with extended endpoints, you can generate types from your own OpenAPI spec:

Type Utilities

Create utility types for common use cases:

Best Practices

Regenerate types regularly when:
  • CoCart releases a new version
  • You update your CoCart plugin
  • API endpoints change
Commit generated types to version control so all team members have access:
Keep generated types separate from custom types:
Create type guards for runtime type checking:

Troubleshooting

Types Not Generating

If type generation fails:
  1. Check your internet connection (for remote URLs)
  2. Verify the OpenAPI spec URL is correct
  3. Ensure openapi-typescript is installed:
  4. Try with verbose output:

Type Errors in IDE

If your IDE shows type errors:
  1. Restart your TypeScript server in VS Code: Cmd/Ctrl + Shift + P → “TypeScript: Restart TS Server”
  2. Check tsconfig.json includes the types directory:
  3. Regenerate types to ensure they’re up-to-date

Missing Types

If some types are missing:
  • The OpenAPI spec may not include all schemas
  • Check if the endpoint exists in the specification file
  • You may need to create custom types for extended functionality

Next Steps

Now that you have TypeScript types set up:
  1. Implement cart functionality with type safety
  2. Add user authentication with typed requests
  3. Build a checkout flow with full type coverage
  4. Explore the API Reference to see all available endpoints

Resources

openapi-typescript Docs

Official documentation for openapi-typescript

CoCart API Reference

Complete API documentation with all endpoints

TypeScript Handbook

Learn more about TypeScript

OpenAPI Specification

OpenAPI specification documentation