Skip to content

Cannot extend the Strapi interface anymore #17365

@luke-z

Description

@luke-z

Bug report

Required System information

  • Node.js version: 18.15.0
  • yarn version: 1.22.19
  • Strapi version: 4.11.5
  • Database: sqlite
  • Operating system: Ubuntu WSL on Windows 11
  • Is your project Javascript or Typescript: Typescript

Describe the bug

I'm currently trying to extend the Strapi interface with custom keys in typescript.
I've seen that this feature has been implemented with pr #13108 but it doesn't seem to work anymore.

Steps to reproduce the behavior

  1. Create a new project with typescript
  2. Add the index.d.ts as mentioned in the pr
import '@strapi/strapi'

declare module '@strapi/strapi' {
    interface StrapiInterface {
        customAddMethod(n1: number, n2: number): number;
    }
}
  1. Set and use the custom method in the src/index.ts file
export default {
  register({ strapi }: { strapi: Strapi }) {
    // No type error when trying the access & set the `customAddMethod` method
    strapi.customAddMethod = (n1, n2) => {
      return n1 + n2;
    };
  },

  bootstrap({ strapi }: { strapi: Strapi }) {
    const result = strapi.customAddMethod(11, 31);

    // This should print "42"
    console.log(result);
  },
};

When not importing the Strapi type, the error is: "Cannot use namespace 'Strapi' as a type."
When importing it via import { Strapi } from '@strapi/strapi, the error is "Property 'customAddMethod' does not exist on type 'Strapi'"

Expected behavior

The newly added function should be available on the Strapi interface and not cause any typing problems.

Am I doing something wrong or is this currently not working anymore?
If it's currently not possible, is there an ETA for when it could be possible?

Metadata

Metadata

Assignees

Labels

issue: bugIssue reporting a bugseverity: mediumIf it breaks the basic use of the product but can be worked aroundsource: typescriptSource is related to TypeScript (typings, tooling, ...)status: pending reproductionWaiting for free time to reproduce the issue, or more information

Type

No type

Projects

Status

Fixed/Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions