I know a wide number of you use TypeScript; well one of the irritations I've had with TypeScript -- I just figured out how to solve. Finally did some research and tests to figure out how to make TypeScript support using ~/ as a normal path for building and determining editor intellisense since this is a special path in NativeScript meaning the home app path. Using this path in a import / require statement means you can do something like this.
and it will load in the file at /app/support/animation
import * as animation from '~/support/animation'
You can use relative paths, but I find absolute path's a lot easier to read and understand exactly which file is being loaded. In addition things like my NativeScript-Updater can't use relative path's (do to some low level issues in the iOS runtimes) and determine if a file has been updated.
Ok, so the solution: open your tsconfig.json file and add the following:
To the "compilerOptions" key in the json file.
When you are trying to access any of the native android platform using TypeScript, TS will complain about the root name --
TS2304: Cannot find name 'zzzz' at line yyyy
TypeScript is unaware that those variables exist and are actually global in scope; so to make TypeScript happy; in your code you will want to do something like this:
declare var android: any;
declare var com: any;
declare var java: any;
This will tell TypeScript that these are global, so it will no longer throw an error when you try to access any classes that start with android, java, or com. If you are accessing any other classes that Android publishes, you can also use the same technique: declare var zzzz: any;
Small update; Josh mentioned it via twitter about installing the tns-platform-declarations. I'm not sure why I didn't post this here also -- so I'm updating the post a tad -- installing the tns-platform-declarations will get you intellisense for the android & ios platforms. It is well worth the download if you are using native ios or android calls.
One of my debugging tools when something doesn't seem to work as expected on iOS is to tell NativeScript to output the metadata while it is building the application. The easiest way to do it is:
TNS_DEBUG_METADATA_PATH="$(pwd)/metadata" tns build ios
This will create a metadata-xxxx folder (where xxx is the platform) in the root of your project. This folder will contain an XML file for EACH item it generated meta data for inside your project. Please note this DOES slow down the build time as it has to create each file. But it is worth it for the diagnostics it provides.
Then I just look at the meta data and I can see if it first created a file for the plugin I'm working on, and if so, what is the exact exposed methods.
The second tip is for those people using TypeScript; if you would like the .d.ts file for your plugin you can do:
TNS_TYPESCRIPT_DECLARATIONS_PATH="$(pwd)/tsc" tns build ios
And it will create a tsc folder with the .d.ts files for each of the items it built metadata for.