analytics
analytics copied to clipboard
Property <<plugin name>> does not exist on type 'Plugins'
Hey! I'm creating a new plugin for the analytics
lib :)
It's almost ready to be published, but I'm getting this Typescript error when calling custom methods in my plugin:
I'm uncertain if that's something I missed in my configuration, or if that's something we can improve in the main analytics
lib. I can prepare a PR here too.
Mmmm not sure how exactly you'd type this.
Suggestions welcome.
Hey :)
So I've found this script to generate typing, but I have hard time understanding it (thus, creating a PR): https://github.com/DavidWells/analytics/blob/e324d0c97797c5d661bb11c8f2014e05cb639564/packages/analytics-core/scripts/types.js
But the simplest solution would be to allow Record
for plugins
property on AnalyticsInstance
like this:
export interface AnalyticsInstance {
identify: Identify;
track: Track;
page: Page;
user: User;
reset: Reset;
ready: Ready;
on: On;
once: Once;
getState: GetState;
storage: Storage;
plugins: Plugins & Record<string, any>;
}
Now plugins
property is typed like this: plugins: Plugins;
Using namespace and interface merging via globals is how my team accomplished this. We did unfortunately have to coerce to get it working if did some wrapping you could be probably get it to override directly.
It would be a combination of @wlecki and a global declaration file.
@shafferchance-lulu could you please share a snippet of our namespace and interface merging via globals? Thanks!
Vite does a similar thing: https://vitejs.dev/guide/env-and-mode