Replies: 8 comments 1 reply
-
By default all capacitor core plugins are typed, I get correct auto completion and checks during tsc. If you are referring to custom plugins one approach could be: import { Plugins } from '@capacitor/core';
/**
* Types
*/
interface GoogleUser {
idToken: string;
}
interface GoogleAuthInterface {
signIn: () => Promise<GoogleUser>;
signOut: () => Promise<void>;
}
/**
* Plugin
*/
const { GoogleAuth } = Plugins;
export default GoogleAuth as GoogleAuthInterface; And just import all your custom plugins from files like |
Beta Was this translation helpful? Give feedback.
-
Yes, I am only referring to "custom" plugins (plugins that are not included in the Capacitor package itself). |
Beta Was this translation helpful? Give feedback.
-
This is related to #1547 |
Beta Was this translation helpful? Give feedback.
-
how are you resolving this issue when using this work around?
|
Beta Was this translation helpful? Give feedback.
-
@aaronksaunders literally JUST ran in to this, by clicking around in current capacitor plugin repos we're supposed to declare as Edit: Edit 2: |
Beta Was this translation helpful? Give feedback.
-
thanks @fkirc, the "Cast Plugin To An Interface" workaround in the OP helped just now with a custom capacitor plugin i'm working on. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much, fkirc. I have been struggling with this issue and searching for solutions for a long time already. I would really appreciate if someone from the Capacitor team could shed some light on this. Is the solution provided by fkirc the right one? Is there a better recommended solution? Would be great, if the plugin documentation explained this. What is the recommended way to consume other interfaces exported by a custom plugin? Using fkirc's solution, this is what I currently do:
Thank you very much. |
Beta Was this translation helpful? Give feedback.
-
In Capacitor 2.x you have to cast to your interface. It isn't optimal, but it isn't the end of the world. In Capacitor 3 you will import plugins directly from a package, no casting will be necessary. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
To use Capacitor plugins, common sample code looks like this:
Unfortunately, this sample code lacks type safety. The TypeScript compiler does not know whether the plugin
NFC
or the functionshowSettings
exists. This has two problematic outcomes:(To illustrate this problem, I chose the plugin https://github.com/adrynov/Capacitor-NFC-Plugin, but this could be another plugin as well.)
Background
The reason why the above code snippet still compiles is a little hack in the Capacitor internals.
Specifically,
core/src/core-plugin-definitions.ts
contains the followingPluginRegistry
:As we can see, only a few predefined plugins are properly typechecked, but not any custom plugins.
Later on, to check whether a plugin is available at runtime, Capacitor checks for the existence of a property in this PluginRegistry:
Workaround: Cast Plugin To An Interface
Many plugin authors provide complete TypeScript definitions for their plugin.
Therefore, we should achieve type safety by importing this definition in our Ionic project.
However, it is not entirely obvious how to import and use this definition.
According to the the docs in https://capacitor.ionicframework.com/docs/plugins/, plugin authors should write definitions that are similar to the following snippet:
The problem is that the above snippet is not sufficient to achieve type safety with the tutorial snippets. Instead, I succeeded to achieve type safety with the following plugin cast and imports:
Although this solution achieves the goal of type safety, it is still suboptimal.
Firstly,
NFCPlugin
is an undocumented interface that could have any other name as well. If users are expected to actually import the interfaceNFCPlugin
, then this should be documented in the README.Secondly, I am not sure whether this solution works well for all custom plugins.
This leads to my final questions:
Beta Was this translation helpful? Give feedback.
All reactions