Skip to content

Commit 6a30996

Browse files
authored
TypeScript: automatic typing generation (#1726)
* Add TS config to inline TOC in config. intro * Rewrite TypeScript configuration docs to include config/typescript.ts * Mention auto-types generation in TypeScript development docs * Remove TypeScript mentions from the table * Reword description for `autogenerate` * Rework the whole and TOC to clarify autocomplete/autogenerate * Removed beta badge * Remove autcomplete from title * Simplify TS config. page introduction * Simplify config/typescript.ts intro. * Fix autogenerate behavior description * Remove unnecessary link * Rename "TypeScript configuration" for TOC * Fix header link * Shorten heading link * Revert "Removed beta badge" This reverts commit b51cc99. * Don't mention autogenerate is beta in TypeScript docs Easier to manage for when we'll remove the beta badge in a few days.
1 parent 9f15023 commit 6a30996

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

docusaurus/docs/dev-docs/configurations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ Strapi also offers the following optional configuration options for specific fea
7373
- [public assets](/dev-docs/configurations/public-assets)
7474
- [Single Sign-On](/dev-docs/configurations/sso) <EnterpriseBadge />
7575
- [Role-Based Access Control](/dev-docs/configurations/rbac)
76+
- [TypeScript](/dev-docs/configurations/typescript)

docusaurus/docs/dev-docs/configurations/typescript.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ description: How to configure Strapi for TypeScript development.
55

66
---
77

8-
# TypeScript project configuration
8+
# TypeScript configuration
99

10-
[TypeScript](/dev-docs/typescript)-enabled Strapi applications have a specific [project structure](/dev-docs/project-structure) with the following dedicated folders and configuration files:
10+
[TypeScript](/dev-docs/typescript)-enabled Strapi projects have a specific project structure and handle TypeScript project configuration through [`tsconfig.json` files](#project-structure-and-typescript-specific-configuration-files).
11+
12+
Strapi also has dedicated TypeScript features that are configured [in the `config/typescript.js|ts` file](#strapi-specific-configuration-for-typescript).
13+
14+
## Project structure and TypeScript-specific configuration files
15+
16+
TypeScript-enabled Strapi applications have a specific [project structure](/dev-docs/project-structure) with the following dedicated folders and configuration files:
1117

1218
| TypeScript-specific directories and files | Location | Purpose |
1319
|-------------------------------------------|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -16,3 +22,40 @@ description: How to configure Strapi for TypeScript development.
1622
| `tsconfig.json` file | application root | Manages TypeScript compilation for the server. |
1723
| `tsconfig.json` file | `./src/admin/` | Manages TypeScript compilation for the admin panel. |
1824

25+
## Strapi-specific configuration for TypeScript <BetaBadge />
26+
27+
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.
28+
29+
To avoid having to [manually generate types](/dev-docs/typescript#generate-typings-for-project-schemas) every time the server restarts, an optional `config/typescript.js|ts` configuration file can be added, which currently accepts only one parameter:
30+
31+
| Parameter | Description | Type | Default |
32+
| -------------- | -------------------------------------------------------------- | --------- | ------- |
33+
| `autogenerate` | Enable or disable automatic types generation on server restart | `Boolean` | `false` |
34+
35+
**Example:**
36+
37+
<Tabs groupId="js-ts">
38+
39+
<TabItem value="javascript" label="JavaScript">
40+
41+
```js title="./config/typescript.js"
42+
43+
module.exports = ({ env }) => ({
44+
autogenerate: true,
45+
});
46+
```
47+
48+
</TabItem>
49+
50+
<TabItem value="typescript" label="TypeScript">
51+
52+
```ts title="./config/typescript.ts"
53+
54+
export default ({ env }) => ({
55+
autogenerate: true,
56+
});
57+
```
58+
59+
</TabItem>
60+
61+
</Tabs>

docusaurus/docs/dev-docs/typescript.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ yarn strapi ts:generate-types --verbose #optional flag
8787
</TabItem>
8888
</Tabs>
8989

90+
:::tip Tip: Automatically generate types
91+
Types can be automatically generated on server restart by adding `autogenerate: true` to [the `config/typescript.js|ts` configuration file](/dev-docs/configurations/typescript#strapi-specific-configuration-for-typescript).
92+
:::
93+
9094
## Develop a plugin using TypeScript
9195

9296
New plugins can be generated following the [plugins development documentation](/dev-docs/plugins-development). There are 2 important distinctions for TypeScript applications:

0 commit comments

Comments
 (0)