Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova plugins in Ionic 1 app using Typescript

I'm new to Typescript. I just started an Ionic 1.2.4 (Angular) project using Typescript. When transpiling, I receive the error message Property 'Keyboard' does not exist on type 'CordovaPlugins' due to the following function passed to angular.module.run() in a file run.ts

///<reference path="../../typings/tsd.d.ts"/>

export function onRun($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      StatusBar.styleDefault();
    }
  })
}

The cordova plugin is in fact installed and it's type definition file definitely exists. Here is the file tsd.d.ts.

/// <reference path="angularjs/angular.d.ts" />
/// <reference path="cordova/cordova.d.ts" />
/// <reference path="cordova/plugins/BatteryStatus.d.ts" />
/// <reference path="cordova/plugins/Camera.d.ts" />
/// <reference path="cordova/plugins/Contacts.d.ts" />
/// <reference path="cordova/plugins/Device.d.ts" />
/// <reference path="cordova/plugins/DeviceMotion.d.ts" />
/// <reference path="cordova/plugins/DeviceOrientation.d.ts" />
/// <reference path="cordova/plugins/Dialogs.d.ts" />
/// <reference path="cordova/plugins/FileSystem.d.ts" />
/// <reference path="cordova/plugins/FileTransfer.d.ts" />
/// <reference path="cordova/plugins/Globalization.d.ts" />
/// <reference path="cordova/plugins/InAppBrowser.d.ts" />
/// <reference path="cordova/plugins/Keyboard.d.ts" />
/// <reference path="cordova/plugins/Media.d.ts" />
/// <reference path="cordova/plugins/MediaCapture.d.ts" />
/// <reference path="cordova/plugins/NetworkInformation.d.ts" />
/// <reference path="cordova/plugins/Push.d.ts" />
/// <reference path="cordova/plugins/Splashscreen.d.ts" />
/// <reference path="cordova/plugins/StatusBar.d.ts" />
/// <reference path="cordova/plugins/Vibration.d.ts" />
/// <reference path="cordova/plugins/WebSQL.d.ts" />
/// <reference path="ionic/ionic.d.ts" />
/// <reference path="jquery/jquery.d.ts" />

I have also tried placing the type definition for Keyboard directly in run.ts. ///<reference path="../../typings/cordova/plugins/Keyboard.d.ts"/>

I'm not expecting the plugin to actually register because 'cordova.js' is not available until the app is built/packaged. I would however like to know how to get Typescript to recognize that Keyboard does in fact exist on cordova.plugins via type defs. Otherwise, how can I avoid this error during transpilation?

like image 331
Jbird Avatar asked Jan 15 '16 22:01

Jbird


People also ask

Does Ionic still support Cordova?

While developers can still use Cordova in the Ionic stack, the future is Ionic with Capacitor (or Capacitor on its own with any popular web stack!). These apps are known as Web Native apps, in contrast to the older hybrid approach.


1 Answers

When installing typescript definitions, I did not install cordova-ionic. The "standard" cordova Keyboard plugin is distinct from the cordova-ionic Keyboard plugin.

tsd install cordova-ionic --save fixed the issue.

Whoops.

like image 168
Jbird Avatar answered Sep 19 '22 04:09

Jbird