TypeScript is an open-source programming language developed by Microsoft, and it is a typed superset of JavaScript that compiles to plain JavaScript. NativeScript, on the other hand, is a framework for building native mobile applications using JavaScript and TypeScript. In this tutorial, we will discuss how to use TypeScript with NativeScript to build native mobile applications.


Prerequisites

Before we dive into TypeScript and NativeScript, we need to have some prerequisites. These are:

  • Node.js and npm (Node Package Manager) installed on your computer.
  • NativeScript CLI (Command Line Interface) installed on your computer.
  • A text editor or Integrated Development Environment (IDE) for writing TypeScript code. Some popular options include Visual Studio Code, WebStorm, and Sublime Text.


Installing NativeScript with TypeScript

The first step is to install NativeScript with TypeScript. We can do this by running the following command in our terminal or command prompt:

npm install -g nativescript

This will install the latest version of NativeScript on our computer. To verify that we have installed NativeScript correctly, we can run the following command:

tns --version

This will display the version of NativeScript installed on our computer.


Creating a New NativeScript Project with TypeScript

To create a new NativeScript project with TypeScript, we can run the following command:

tns create my-app --template tns-template-blank-ts

This will create a new NativeScript project with TypeScript and the blank template. We can replace my-app with the name of our project.


Writing TypeScript Code

Now that we have set up our project, we can start writing TypeScript code. NativeScript uses the TypeScript compiler to compile TypeScript code into JavaScript. This means that we can use all the features of TypeScript, such as types, interfaces, classes, and modules, in our NativeScript application.

Let's create a simple NativeScript application that displays a message when the user taps a button. First, we need to create a new page. We can do this by creating a new file in the app folder with the .page.ts extension. Let's call this file main.page.ts.

In main.page.ts, we can write the following code:

import { Page } from "@nativescript/core";

export class MainPage extends Page {
    constructor() {
        super();
    }

    public onButtonTap(): void {
        console.log("Button tapped!");
    }
}

In this code, we import the Page class from the @nativescript/core module. We then define a new class called MainPage that extends the Page class. We override the constructor of the Page class and define a new method called onButtonTap that logs a message to the console when the user taps a button.

Next, we need to create a new XML file that defines the layout of the page. We can create a new file in the app folder with the .page.xml extension. Let's call this file main.page.xml.

In main.page.xml, we can write the following code:

<Page xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:ui="nativescript-ui-listview">
    <StackLayout>
        <Button text="Tap me!"
                tap="{{ onButtonTap }}" />
    </StackLayout>
</Page>

In this code, we define a new Page element and a StackLayout element that contains a Button element. We set the text of the button to "Tap me!" and bind the tap event to the onButtonTap method of the MainPage class.

Finally, we need to register the page with the NativeScript framework. We can do this by adding the following code to the app.module.ts file:

import { NgModule } from "@angular/core";
import { NativeScriptModule } from "@nativescript/angular";
import { NativeScriptFormsModule } from "@nativescript/angular/forms";
import { NativeScriptRouterModule } from "@nativescript/angular/router";

import { AppComponent } from "./app.component";
import { MainPage } from "./main.page";

@NgModule({
    declarations: [AppComponent, MainPage],
    imports: [
        NativeScriptModule,
        NativeScriptFormsModule,
        NativeScriptRouterModule,
    ],
    providers: [],
    bootstrap: [AppComponent],
})
export class AppModule {}

In this code, we import the MainPage class and add it to the declarations array. We also import several NativeScript modules and add them to the imports array. Finally, we bootstrap the AppComponent class, which is the entry point of our application.


Running the Application

To run our application, we can use the following command:

tns run android --bundle

This will build our application and run it on an Android device or emulator. If we want to run our application on an iOS device or emulator, we can use the following command:

tns run ios --bundle


Conclusion

In this tutorial, we have discussed how to use TypeScript with NativeScript to build native mobile applications. We have seen how to create a new NativeScript project with TypeScript, how to write TypeScript code, and how to run the application on Android and iOS devices. With the power of TypeScript and NativeScript, we can build high-quality, native mobile applications with ease.