TypeScript Development with Strapi
The content of this page might not be fully up-to-date with Strapi 5 yet.
While developing a TypeScript-based application with Strapi, you can:
- access typings for the
Strapi
class with autocompletion, - generate typings for your project's content-types,
- start Strapi programmatically,
- and follow some TypeScript-specific instructions for plugins development.
Use Strapi
TypeScript typings
Strapi provides typings on the Strapi
class to enhance the TypeScript development 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:
-
Open the
./src/index.ts
file from your code editor. -
Declare the
strapi
argument as typeStrapi
within the globalregister
method:./src/index.tsimport { Strapi } from '@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
runLifecyclesFunctions
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 content-types schemas
To generate typings for your project schemas use the ts:generate-types
CLI command. The ts:generate-types
command creates the folder types
, at the project root, which stores the typings for your project. The optional --debug
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:
- npm
- yarn
npm run strapi ts:generate-types --debug #optional flag to display additional logging
yarn strapi ts:generate-types --debug #optional flag to display additional logging
Types can be automatically generated on server restart by adding autogenerate: true
to the config/typescript.js|ts
configuration file.
To use Strapi types in your front-end application, you can use a workaround until Strapi implements an official solution.
Fix build issues with the generated types
The generated types can be excluded so that the Entity Service doesn't use them and falls back on looser types that don't check the actual properties available in the content types.
To do that, edit the tsconfig.json
of the Strapi project and add types/generated/**
to the exclude
array:
// ...
"exclude": [
"node_modules/",
"build/",
"dist/",
".cache/",
".tmp/",
"src/admin/",
"**/*.test.ts",
"src/plugins/**",
"types/generated/**"
]
// ...
However, if you still want to use the generated types on your project, but don't want Strapi to use them, a workaround could be to copy those generated types and paste them outside of the generated
directory (so that they aren't overwritten when the types are regenerated) and remove the declare module '@strapi/types'
from the bottom of the file.
Types should only be imported from @strapi/strapi
to avoid breaking changes. The types in @strapi/types
is for internal use only and is subject to change without notice.