# TypeScript development
TypeScript adds an additional type system layer above JavaScript, which means that existing JavaScript code is also TypeScript code. Strapi supports TypeScript in new projects on v4.2.0 and above. TypeScript-enabled projects allow developing plugins with TypeScript as well as using TypeScript typings.
🤓 Getting started with TypeScript
To start developing in TypeScript, use the CLI installation documentation to create a new TypeScript project. Additionally, the project structure and TypeScript configuration sections have TypeScript-specific resources for understanding and configuring an application.
🚧 JavaScript to TypeScript migration
Migrating existing Strapi applications written in JavaScript is not currently recommended. In the meantime, feel free to ask for help on the forum (opens new window) or the community Discord (opens new window).
# Start developing in TypeScript
Starting the development environment for a TypeScript-enabled project requires building the admin panel prior to starting the server. In development mode, the application source code is compiled to the ./dist/build
directory and recompiled with each change in the Content-type Builder. To start the application, run the following commands in the root directory:
# Use TypeScript typings
Strapi provides typings on the Strapi
class to improve the TypeScript developing experience. These typings come with an autocomplete feature that automatically offers suggestions while developing.
To experience TypeScript-based autocomplete while developing Strapi applications, you could try the following:
From your code editor, open the
./src/index.ts
file.In the
register
method, declare thestrapi
argument as of typeStrapi
:// path: ./src/index.ts import '@strapi/strapi'; export default { register( { strapi }: { strapi: Strapi }) { // ... }, };
Within the body of the
register
method, start typingstrapi.
and use keyboard arrows to browse the available properties.Choose
runLifecyclesfunction
from the list.When the
strapi.runLifecyclesFunctions
method is added, a list of available lifecycle types (i.e.register
,bootstrap
anddestroy
) are returned by the code editor. Use keyboard arrows to choose one of the lifecycles and the code will autocomplete.
# Generate typings for project schemas
To generate typings for your project schemas use the ts:generate-types
CLI command. The ts:generate-types
command creates the file schemas.d.ts
, at the project root, which stores the schema typings. The optional --verbose
flag returns a detailed table of the generated schemas.
To use ts:generate-types
run the following code in a terminal at the project root:
# Develop a plugin using TypeScript
New plugins can be generated following the plugins development documentation. There are 2 important distinctions for TypeScript applications:
- After creating the plugin, run
yarn
ornpm install
in the plugin directorysrc/admin/plugins/[my-plugin-name]
to install the dependencies for the plugin. - Run
yarn build
ornpm run build
in the plugin directorysrc/admin/plugins/[my-plugin-name]
to build the admin panel including the plugin.
✏️ NOTE
It is not necessary to repeat the yarn
or npm install
command after the initial installation. The yarn build
or npm run build
command is necessary to implement any plugin development that affects the admin panel.
# Start Strapi programmatically
To start Strapi programmatically in a TypeScript project the Strapi instance requires the compiled code location. This section describes how to set and indicate the compiled code directory.
# Use the strapi()
factory
Strapi can be run programmatically by using the strapi()
factory. Since the code of TypeScript projects is compiled in a specific directory, the parameter distDir
should be passed to the factory to indicate where the compiled code should be read:
// path: ./src/plugins/<plugin-name>/server/index.js
const strapi = require('@strapi/strapi');
const app = await strapi({ distDir: './dist' });
# Use the strapi.compile()
function
The strapi.compile()
function should be mostly used for developing tools that need to start a Strapi instance and detect whether the project includes TypeScript code. strapi.compile()
automatically detects the project language. If the project code contains any TypeScript code, strapi.compile()
compiles the code and returns a context with specific values for the directories that Strapi requires:
const strapi = require('@strapi/strapi');
const appContext = await strapi.compile();
const app = await strapi(appContext);