Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Generating TypeScript Types from the OpenAPI Specification

Generating TypeScript Types from the OpenAPI Specification

~14 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

The MANUALLY written types from chapter 75 get OUT OF SYNC AS SOON as the API changes – openapi-typescript generates them AUTOMATICALLY from the specification from chapter 17 INSTEAD.

Installing the tool

npm install --save-dev openapi-typescript

Generation as an npm script

package.json (excerpt)
{
  "scripts": {
    "generate-types": "openapi-typescript https://localhost/api/docs.jsonopenapi --output src/types/api.ts"
  }
}

docs.jsonopenapi is API Platform's JSON endpoint for the RAW OpenAPI specification (EXACTLY the content from chapter 17's api:openapi:export, here fetched DIRECTLY over HTTP instead of via the console).

npm run generate-types

Using the generated types

import type { components } from '../types/api';

type Project = components['schemas']['Project.jsonld-project.read'];
type ProjectCollection = components['schemas']['Project.jsonld-project.read.collection'];

The generated type names CONTAIN the serialization group (chapter 23) – VISIBLE proof that the generated types EXACTLY reflect WHICH fields actually exist in WHICH situation, instead of making a BLANKET assumption.

Replacing the manual type

// hooks/useProjects.ts - BEFORE (chapter 75)
import type { ProjectCollection } from '../types/project';

// AFTER
import type { components } from '../types/api';
type ProjectCollection = components['schemas']['Project.jsonld-project.read.collection'];

Achtung: src/types/api.ts is NOT meant to be edited by hand – the file gets COMPLETELY REWRITTEN on EVERY npm run generate-types, CUSTOM changes would get LOST. In a REAL project, a comment at the top of the file making EXACTLY this clear is worthwhile.

Automation, a preview

In a REAL team, generate-types would be part of the CI workflow (blocks 11-12 cover deployment in depth) – if the API structure CHANGES, a tsc type check FAILS instead of SILENTLY masking wrong assumptions in the frontend.

Tipp: This EXACT tool was ALREADY ANNOUNCED in chapter 17 ("generating client SDKs") – NOW the possibility described there gets CONCRETELY implemented for OUR React project.