How to type the Context object in Strapi API with TypeScript?

I am using the TypeScript configuration for Strapi and trying to type the Context object (ctx) in my controller handlers. The docs feature no example of typing it and there’s nothing online about it either. In JS, the ctx object features intellisense because it says it’s typed as “Application.Context” but I can’t find that namespace anywhere.

Heres an example of what I want to do:

import { factories } from "@strapi/strapi";
import { Strapi } from "@strapi/strapi";
export default factories.createCoreController(
  ({ strapi }: { strapi: Strapi }) => ({
    async create(ctx) {

I would like the ctx object to display autocompletion suggestions to me from knowing what properties are on the ctx object.

System Information
  • Strapi Version: 4
  • Operating System: Debian (WSL)
  • Database: SQLITE
  • Node Version: 18.x.x
  • NPM Version:
  • Yarn Version:

I am having a similar issue, as I see here, it seems that Strapi does not support Typescript properlly yet, but they are improving this support.

At the moment, I believe we need to implement the type ourselves if we want it.

I installed the @types/koa types and use the Context type.

import { Strapi } from "@strapi/strapi";
import { Context } from "koa";

export default ({ strapi }: { strapi: Strapi }) => ({
  async findOne(ctx: Context) {
    // ...

To type the Context object in a Strapi API with TypeScript, you can follow these steps:
import { DefaultContext } from ‘koa’;

declare module ‘koa’ {
interface DefaultContext extends StrapiContext { }

interface StrapiContext {
// Declare any additional properties or methods you want to add to the Context object.
// For example, if you have a custom service in your Strapi API, you can add its type here.
myCustomService: MyCustomService;

If Strapi does not support Typescript, why do they have an option to create Strapi project with typescript:

Hey @huylv177, that is true, they definetely support it. I think I expressed what I wanted badly on the previous comment, thanks for clearing that out!

I just felt like Strapi should be exporting all the types related to the variables of Strapi, but that does not happen for the Context, for example. But the solution provided here works well to type the Context :tada:

1 Like