@datocms/cma-schema-types-generator

中文文档
6.7.0发布于 2026/09/24MITTypeScriptESM + CJS近 30 天下载 9.5万

Generate TypeScript type definitions from a DatoCMS project schema

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Example output
  4. Type-and-value declaration merging
  5. Options
  6. Migration variant

@datocms/cma-schema-types-generator

Generate TypeScript type definitions from a DatoCMS project schema.

Produces ItemTypeDefinition-based types (one per model/block) plus AnyBlock / AnyModel / AnyBlockOrModel union types, ready to be used with @datocms/cma-client for type-safe content management and migration scripts.

Installation

npm install @datocms/cma-schema-types-generator

@datocms/cma-client is a peer dependency.

Usage

import { buildClient } from '@datocms/cma-client';
import { generateSchemaTypes } from '@datocms/cma-schema-types-generator';

const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN! });
const code = await generateSchemaTypes(client);

code is a string containing prettier-formatted TypeScript, ready to be written to disk:

import { writeFile } from 'node:fs/promises';

await writeFile('src/generated/schema.ts', code);

Example output

import type { ItemTypeDefinition } from '@datocms/cma-client';

type EnvironmentSettings = {
  locales: 'en' | 'it';
};

export type BlogPost = ItemTypeDefinition<
  EnvironmentSettings,
  '12345',
  {
    title: { type: 'string'; localized: true };
    content: { type: 'rich_text'; blocks: Hero };
  }
>;
export const BlogPost = {
  ID: '12345',
  REF: { type: 'item_type', id: '12345' },
} as const;

export type Hero = ItemTypeDefinition<EnvironmentSettings, '67890', { /* … */ }>;
export const Hero = {
  ID: '67890',
  REF: { type: 'item_type', id: '67890' },
} as const;

export type AnyBlock = Hero;
export type AnyModel = BlogPost;
export type AnyBlockOrModel = AnyBlock | AnyModel;

Type-and-value declaration merging

Each item type is emitted twice: once as a TypeScript type (the ItemTypeDefinition<…> alias) and once as an export const carrying its runtime metadata. TypeScript merges the two — Schema.BlogPost resolves both as a type and as a value, depending on the position it's used in.

The runtime const exposes two fields:

  • ID — the bare item type id (e.g. '12345'), typed as a string literal so it can be used for narrowing.
  • REF — the JSON:API resource identifier object { type: 'item_type', id: '<sameId>' }, ready to be passed wherever the API expects an item type reference.

This lets call sites stay free of hardcoded string ids:

import { buildClient } from '@datocms/cma-client';
import * as Schema from './generated/schema';

const client = buildClient({ apiToken: process.env.DATOCMS_API_TOKEN! });

await client.items.create<Schema.LandingPage>({
  item_type: Schema.LandingPage.REF,
  title: 'Hello world',
});

if (block.__itemTypeId === Schema.HeroBlock.ID) {
  // narrowed to Schema.HeroBlock
}

Options

Both entry points accept an optional SchemaTypesGeneratorOptions object:

  • itemTypesFilter — comma-separated list of model api keys to include. Block dependencies referenced by the kept models (through rich_text, structured_text, single_block) are pulled in automatically. Unknown api keys are ignored.
  • format — formats the output with Prettier. Defaults to true. Prettier runs in a worker thread whose memory does not go back to the main process: on a project with thousands of fields this adds hundreds of megabytes to the peak memory use. Set it to false when the code goes to a compiler and not to a person. The TypeScript printer already emits indented, multi-line code; only the quote style and the line breaks are different.

Migration variant

generateSchemaTypesForMigration produces the same declarations without the import type { ItemTypeDefinition } line — meant to be inlined directly inside a migration script that already has access to the type.

import { generateSchemaTypesForMigration } from '@datocms/cma-schema-types-generator';

const inline = await generateSchemaTypesForMigration(client, {
  itemTypesFilter: 'blog_post,author',
});

版本与发布记录

@datocms/cma-schema-types-generator 共发布 27 个版本,首次发布于 2026/04/29;过去 12 个月发布了 27 个版本。

版本发布日期距今
6.7.0latest2026/09/241 个月前
6.5.02026/09/181 个月前
6.1.22026/08/312 个月前
6.1.02026/08/312 个月前
6.0.02026/08/312 个月前
5.8.02026/08/242 个月前

其他发布通道:next → 5.5.0-alpha.2。

关于 @datocms/cma-schema-types-generator 的常见问题

@datocms/cma-schema-types-generator 是什么?

Generate TypeScript type definitions from a DatoCMS project schema

如何安装 @datocms/cma-schema-types-generator?

在项目目录运行 npm install @datocms/cma-schema-types-generator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@datocms/cma-client(^6.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@datocms/cma-schema-types-generator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@datocms/cma-schema-types-generator 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@datocms/cma-schema-types-generator 可以商用吗?

@datocms/cma-schema-types-generator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@datocms/cma-schema-types-generator 的下载量有多少?

根据 npm 官方下载统计,@datocms/cma-schema-types-generator 在 2026/10/12 – 2026/11/10 这 30 天内被下载 94,842 次,日均约 3,161 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datocms/cma-schema-types-generator 还在维护吗?

最新版本 6.7.0 发布于 2026/09/24(1 个月前);至今共发布 27 个版本,其中过去 12 个月发布了 27 个;首次发布于 2026/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。