I have encountered a question similar to this before, but I couldn't locate one specifically pertaining to special characters such as @
and /
within namespaces.
Scenario:
I am looking to enhance the
import { Strapi } from '@strapi/strapi';
interface of Strapi
from @strapi/strapi.
It lacks the webhookStore
and other properties that I want to include.
Initial approach
I created a file named extensions.d.ts
with the following content
declare global {
namespace '@strapi/strapi' {
interface Strapi {
webhookStore: {
c: any;
};
}
}
}
Unfortunately, it did not work. Typescript raised an error TS1003: Identifier expected.
for '@strapi/strapi'
https://i.sstatic.net/Sa2PB.png
In the past, I successfully extended interfaces with single names like "Stripe", however, in this scenario, I am unsure how to proceed.
I cannot use @strapi/strapi without encasing it in '
as it leads to compilation errors.
Alternate approach - module augmentation/merging
I also attempted the concept of module augmentation
suggested here, but it was unsuccessful.
type WebhookStore = {
findWebhooks: () => void;
};
declare module '@strapi/strapi' {
interface Strapi {
webhookStore: WebhookStore;
}
}
This essentially redeclares the Strapi interface and only recognizes webhookStore as the sole property! (losing information about other properties exported by the library itself). Furthermore, it fails to compile as the ts compiler only acknowledges webhookStore. It is simply redeclaring without merging.
As a result, numerous errors like this occur during yarn build
, which involves linting and compiling
5 import { factories } from '@strapi/strapi';
~~~~~~~~~
src/api/tag/routes/tag.ts:5:10 - error TS2305: Module '"@strapi/strapi"' has no exported member 'factories'.