Nativescript: Typescript 2.0.7 can't find 'android' global

Created on 5 Nov 2016  路  8Comments  路  Source: NativeScript/NativeScript

Which platform(s) does your issue occur on?

Android / Typescript 2.0.7

Please provide the following version numbers that your issue occurs with:

  • CLI: 2.3.0
  • Cross-platform modules: 2.3.0
  • Runtime(s): 2.3.0
  • Plugin(s):
    "nativescript-geolocation": "0.0.14",
    "nativescript-googlemaps": "0.0.6",
    "nativescript-theme-core": "^0.1.4",
    "nativescript-unit-test-runner": "^0.3.3",
    "tns-core-modules": "^2.3.0"

Please tell us how to recreate the issue in as much detail as possible.

android global is not defined, couldn't find it anywhere in node_modules
this code example using android global is placed everywhere, even in the NS blog (and stackoverflow)

import { ad } from 'utils/utils';

export class Model extends Observable {
   constructor() {
      super()

      let context = ad.getApplicationContext()
      this.number = context.getSystemService(android.content.Context.TELEPHONY_SERVICE).getLine1Number()
   }
}
question

Most helpful comment

Have you tried adding to the _references.d.ts_ file, I believe it should work. I actually haven't used the platform defs in a few months so I'm not positive on any changes but I do know people on the Slack channel use them and the question has come up before about how to use the native apis and once they install and add the ///ref they report that it works.

All 8 comments

You'll need the platform declarations for TS to know about it. Theyre on npm from the NS team. 'tns-platform-declarations' I believe is the package name.

thanks @bradmartin couldn't find any information about it anywhere though

EDIT: doesn't work, it seems its the stripped down version of the source in tns-core-modules. there's no android namespace or global there, but they are being referenced in org.nativescript.widgets.d.ts

android

Did you install this package tns-platform-declarations to your project? Once you do, you'll need to add the ref in _references.d.ts_ in the project root so TS knows about it. If that's unclear see this answer: http://stackoverflow.com/a/37524018/1893557 which brings up a good learning point if you're unsure why you need this. If you don't know and want to know why just let me know, otherwise I won't waste your time 馃槃

Yes, and I'm using the types setting in tsconfig.json

There's no android.d.ts, only android17.d.ts, but the module isn't being exported though, does it have support for TS 2.0? I had to do import 'tns-platform-declarations/tns-core-modules/android17' at the top of the file

Have you tried adding to the _references.d.ts_ file, I believe it should work. I actually haven't used the platform defs in a few months so I'm not positive on any changes but I do know people on the Slack channel use them and the question has come up before about how to use the native apis and once they install and add the ///ref they report that it works.

it does indeed, so I guess it's not meant to use with newer versions of Typescript. does it have a documentation anywhere? the API reference search in NS site doesn't work

Hey @pocesar , @bradmartin

The README.md for tns-platform-declarations has a detailed instruction no how to use them with instructions on what to add in references.d.ts and in tsconfig.json

Direct Link: https://github.com/NativeScript/NativeScript/tree/master/tns-platform-declarations

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings