# @contentstack/types-generator 中文文档

规范地址：https://npmcn.com/package/%40contentstack/types-generator/

版本：3\.10\.6
npm 元数据更新时间：2026-10-06T12:17:09.366Z
中文译文更新时间：2026-11-10T16:50:16.508Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40contentstack/types-generator
原文对照：https://npmcn.com/package/%40contentstack/types-generator/?lang=original
项目主页：https://github.com/contentstack/types-generator
源码仓库：https://github.com/contentstack/types-generator

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：158 kB，14 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：6 个
- 首次发布：2024-08-27T06:45:03.582Z
- 最新版本发布：2026-10-06T12:17:08.976Z
- 近 30 天下载量：11593（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：35（近 12 个月 13 个）

## 中文简介

内容tack 类型定义生成库

## 安装

```sh
npm install @contentstack/types-generator
```

也可以使用：`pnpm add @contentstack/types-generator`、`yarn add @contentstack/types-generator`、`bun add @contentstack/types-generator`

## README

# 内容ack - TypeScript 生成库

这个库帮助生成 Stack 中可用的内容类型 TypeScript 类型定义 .

## 安装

`$ npm install @contentstack/types-generator`

## 移徙

参见 [移徙指南](https://github.com/contentstack/types-generator/blob/main/MIGRATION.md) 3版,如果您正在从2版或2版以上迁移.

## 使用量

### 在节点J

`require("@contentstack/types-generator")` 联合来文委员会

或

`import {<< required method >>} from "@contentstack/types-generator"` eCMAScript模块(ESM)

### 在 Web 应用程序中

`import {<< required method >>} from "@contentstack/types-generator/dist/web"`

## 使用指南

#### 1\. 联合国. `generateTS()` (可用于节点JS和Web应用程序)

这是一个同步方法,它利用给定输入生成 Stack 中可用的内容类型类型定义。 使用此方法用于 `REST API`。 。 。 .

**输入 :**

| 属性名称 | 说明 | 数据类型 | 接受数值 | 强制性 | 默认值 |
| --- | --- | --- | --- | --- | --- |
| 符号 | 用于授权的独特标识符 | 字符串 |  | 对 |  |
| 符号类型 | 正在提供的令牌类型(目前我们只支持送出令牌) | 字符串 | 交付 | 对 |  |
| 键 | 堆叠 API 密钥 | 字符串 |  | 对 |  |
| 环境 | 环境名称(例如:开发、中转、生产) | 字符串 |  | 对 |  |
| 区域 | 目录 API 区域 | 字符串 | 美国(AWS NA)、欧盟(AWS 欧盟)、非盟(AWS AU)、AZURE NA、AZURE EU、GCP NA | 对 |  |
| 分支 | 堆栈分支名称 | 字符串 |  | 没有 |  |
| 前缀 | 为每个接口添加的可选前缀 | 字符串 |  | 没有 |  |
| 包括文档 | 在生成文件中添加内容类型文档 | 布尔 | 真实,虚假 | 没有 | 真实 |
| 系统字段 | 布尔旗表示是否在响应中包含系统生成的字段 | 布尔 | 真实,虚假 | 没有 | 虚假 |

**输出 :**

返回以数据解决或以错误拒绝的保证.

**_如果解决:_**

类型:字符串

数据: 生成的 Typescript 类型定义

**_如果被拒绝:_**

类型: 出错对象

数据 : 对象为 `error_message`

**示例用法:** `generateTS()`

```typescript
import { generateTS } from "@contentstack/types-generator"; // Import statement for NodeJS
import { generateTS } from "@contentstack/types-generator/dist/web"; // Import statement for Web application

async function getTypeDef() {
  try {
    const typeDef = await generateTS({
      token: "<< your_delivery_token >>",
      tokenType: "delivery", // Currently we are supporting only delivery token
      apiKey: "<< your_stack_api_key >>",
      environment: "development",
      region: "US",
      branch: "main",
      prefix: "CS",
      includeDocumentation: true,
      systemFields: false,
    });

    // Handle the resolved promise, e.g., process the typeDef
  } catch (error) {
    // Handle the rejected promise
    // error: { error_message: "Unauthorized! Please check the given token and api key" }
  }
}

getTypeDef();
```

**示例输出 :** `generateTS()`

```typescript
/** This is a description. */
interface BuiltinExample {
  /** Title */
  title: string;
  /** URL */
  url: string;
  /** Group1 */
  group1?: {
    /** Group2 */
    group2?: {
      /** Group3 */
      group3?: {
        /** Number */
        number?: number;
      };
    };
  };
  /** SEO */
  seo?: Seo;
  /** Single line textbox */
  single_line?: string;
  /** Multi line textbox */
  multi_line?: string;
  /** Rich text editor */
  rich_text_editor?: string;
  /** Multiple Single Line Textbox */
  multiple_single_line_textbox?: string[];
  /** Markdown */
  markdown?: string;
  /** Multiple Choice */
  multiple_choice?: ("Choice 1" | "Choice 2" | "Choice 3")[];
  /** Single Choice */
  single_choice: "Choice 1" | "Choice 2" | "Choice 3";
  /** Modular Blocks */
  modular_blocks?: ModularBlocks[];
  /** Number */
  number?: number;
  /** Link */
  link?: Link;
  /** File */
  file?: File;
  /** Boolean */
  boolean?: boolean;
  /** Date */
  date?: string;
}

interface ModularBlocks {
  block_1: {
    /** Number */
    number?: number;
    /** Single line textbox */
    single_line?: string;
  };
  block_2: {
    /** Boolean */
    boolean?: boolean;
    /** Date */
    date?: string;
  };
  seo_gf: {
    /** Keywords */
    keywords?: string;
    /** Description */
    description?: string;
  };
}
```

#### 2\. 联合国. `graphqlTS()` (仅供NodeJS使用)

这是一个同步方法,它利用给定的输入为GraphQL生成 Stack 中可用的内容类型类型定义。 使用此方法用于 `GraphQL`。 。 。 .

**输入 :**

| 属性名称 | 说明 | 数据类型 | 接受数值 | 强制性 |
| --- | --- | --- | --- | --- |
| 符号 | 用于授权的独特标识符 。 这应该是堆的送货符. | 字符串 |  | 对 |
| 键 | 堆叠 API 密钥 | 字符串 |  | 对 |
| 环境 | 环境名称(例如:开发、中转、生产) | 字符串 |  | 对 |
| 区域 | 目录 API 区域 | 字符串 | 美国(AWS NA)、欧盟(AWS 欧盟)、非盟(AWS AU)、AZURE NA、AZURE EU、GCP NA | 对 |
| 分支 | 堆栈分支名称 | 字符串 |  | 没有 |
| 命名空间 | 识别计划内的特定命名空间 | 字符串 |  | 没有 |

**输出 :**

返回以数据解决或以错误拒绝的保证.

**_如果解决:_**

类型:字符串

数据: 生成的 Typescript 类型定义

**_如果被拒绝:_**

类型: 出错对象

数据 : 对象为 `error_message`

**示例用法:** `graphqlTS()`

```typescript
import { graphqlTS } from "@contentstack/types-generator"; // Import statement for NodeJS

async function getTypeDef() {
  try {
    const typeDef = await graphqlTS({
      token: "<< your_delivery_token >>", // Currently we are supporting only delivery token
      apiKey: "<< your_stack_api_key >>",
      environment: "development",
      region: "US",
      branch: "main",
      namespace: "<< your_name_space >>",
    });

    // Handle the resolved promise, e.g., process the typeDef
  } catch (error) {
    // Handle the rejected promise
    // error: { error_message: "Unauthorized! Please check the given token and api key" }
  }
}

getTypeDef();
```

## 常见问题

### @contentstack/types-generator 是什么？

内容tack 类型定义生成库

### 如何安装 @contentstack/types-generator？

在项目目录运行 npm install @contentstack/types-generator，或使用 pnpm add / yarn add / bun add。

### @contentstack/types-generator 支持 TypeScript 吗？

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

### @contentstack/types-generator 支持 ESM 还是 CommonJS？

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

### @contentstack/types-generator 可以商用吗？

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

### @contentstack/types-generator 的下载量有多少？

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

### @contentstack/types-generator 还在维护吗？

最新版本 3\.10\.6 发布于 2026/10/06（1 个月前）；至今共发布 35 个版本，其中过去 12 个月发布了 13 个；首次发布于 2024/08/27。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

