TypeScript 配置
🌐 TypeScript configuration
Page summary:
TypeScript 配置说明了项目的 tsconfig 文件、输出目录以及一个可选的
config/typescript.js|ts,该选项可以在服务器重启时自动生成类型。本文档解释了哪些文件夹存储已编译的代码以及如何切换此实验性功能。
TypeScript 支持的 Strapi 项目具有特定的项目结构,并通过 tsconfig.json 文件 处理 TypeScript 项目配置。
Strapi 也有专门的 TypeScript 功能,这些功能在 config/typescript.js|ts 文件 中进行配置。
🌐 Strapi also has dedicated TypeScript features that are configured in the config/typescript.js|ts file.
项目结构和 TypeScript 特定的配置文件
🌐 Project structure and TypeScript-specific configuration files
启用了 TypeScript 的 Strapi 应用具有特定的 项目结构,包括以下专用文件夹和配置文件:
🌐 TypeScript-enabled Strapi applications have a specific project structure with the following dedicated folders and configuration files:
| TypeScript 特定目录和文件 | 位置 | 目的 || --- | --- | --- || ./dist 目录 | 应用根目录 | 添加用于编译项目 JavaScript 源代码的位置 || build 目录 | ./dist | 包含已编译的管理面板 JavaScript 源代码。该目录在第一次 yarn build 或 npm run build 命令时创建 || tsconfig.json 文件 | 应用根目录 | 管理服务器的 TypeScript 编译 || tsconfig.json 文件 | ./src/admin/ | 管理管理面板的 TypeScript 编译 |
TypeScript 的 Strapi 专用配置
🌐 Strapi-specific configuration for TypeScript
这些设置被视为实验性的,可能会出现问题或破坏某些功能。
🌐 These settings are considered experimental and might have issues or break some features.
Strapi 生成的类型是基于用户项目结构的。一旦类型定义被导出到其专用文件中,Strapi 会读取这些类型定义以相应地调整自动补齐结果。
🌐 Types generated by Strapi are based on the user project structure. Once the type definitions are emitted into their dedicated files, Strapi reads the type definitions to adapt the autocompletion results accordingly.
为了避免每次服务器重启时都必须手动生成类型,可以添加一个可选的 config/typescript.js|ts 配置文件,目前该文件仅接受一个参数:
🌐 To avoid having to manually generate types every time the server restarts, an optional config/typescript.js|ts configuration file can be added, which currently accepts only one parameter:
| 参数 | 描述 | 类型 | 默认值 || --- | --- | --- | --- || autogenerate | 在服务器重启时启用或禁用自动类型生成 | Boolean | false |
示例:
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
autogenerate: true,
});
export default ({ env }) => ({
autogenerate: true,
});