Skip to content

[4.2 beta] We can not extends strapi typescript interfaces #12898

@stafyniaksacha

Description

@stafyniaksacha

Bug report

Describe the bug

Strapi comes with two different typing system:

  • One that is used internally
import type { StrapiInterface } from '@strapi/strapi'

function myFunction(strapi: StrapiInterface) {
  // here we use the internal strapi interface
}
import { factories } from '@strapi/strapi'
const { createCoreController } = factories

export default createCoreController('api::foo:foo', ({ strapi }) => {
  // here we also use the internal strapi interface
}
  • One that is exposed globally
function myFunction() {
  // here we use the global strapi interface
  strapi.log.debug('it works')
}

The interfaces used for internal typing are not exported, so we can not do:

declare module '@strapi/strapi' {
  // use declaration merging
  interface StrapiInterface {
    plugin: (pluginName: 'my-plugin') => any;
  }
}

For now we can do something quite similar for the global part, but il would break all typing provided by arguments on factories.

declare global {
  interface StrapiInterface {
    plugin: (pluginName: string) => any;
  }
  interface AllTypes {
    [key: string]: unknown;
    foo: any;
  }
}

Also the default Strapi Class is not considered in end project because of the type overriding (@strapi/strapi/lib/index.d.ts, when .d.ts file is present, typescript just ignore .js files), so we end up with only query and entityService as Strapi object member:
image

Expected behavior

The internal type should be the reference and the only interface that need to be updated.

Metadata

Metadata

Assignees

Labels

issue: bugIssue reporting a bugseverity: mediumIf it breaks the basic use of the product but can be worked aroundstatus: confirmedConfirmed by a Strapi Team member or multiple community members

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions