TypeScript is a first-class citizen in NativeScript.TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. In addition to the great features you like about JavaScript, TypeScript offers benefits of special interest to developers coming from Strongly Typed, or Object Oriented backgrounds.
Fun Fact: The core NativeScript team uses TypeScript to build the NativeScript framework.
Using TypeScript with NativeScript is easy. From your terminal or command line, just start a new project with this command:
$ tns create my-app-name --template typescript
This tells the NativeScript CLI to create a new project using TypeScript. NativeScript will automatically compile TypeScript (.ts) files to JavaScript when the project is built. You can still use plain JavaScript (.js) files and libraries in a TypeScript project. The compiler will figure it out.
Adding TypeScript support to an existing NativeScript project is easy, too. Just run this command in your existing project root directory:
$tns install typescript
Don’t be intimidated! TypeScript is easy to learn, compiles to “plain old JavaScript” (so it runs everywhere) and can be used for web and mobile projects. It is also the default language choice if you use Angular 2.0. Here are some great resources we recommend to get started:
Reminder: If you don’t want to learn TypeScript, you can still use NativeScript. NativeScript fully supports TypeScript and “vanilla” JavaScript projects and workflows.
NativeScript is built with TypeScript. It’s not an afterthought, it’s how the code is written. That means there is 100% integration with all NativeScript APIs and even all native APIs when you use TypeScript.
Combined with Visual Studio Code, TypeScript gives you powerful intellisense for your NativeScript projects. That means that you can see all available APIs, methods and properties right where you need them - inline with your code and not stuffed somewhere in the docs.
Combined with Visual Studio Code, TypeScript gives you powerful refactoring capabilities. You can change a method/property signature in seconds without worrying that the old name will stay somewhere in the project. This is one of the reasons we decided to use for NativeScript - a large scale project.
NativeScript means native apps. Native apps don’t handle code errors as well as a browser does. TypeScript gives you an extra level of confidence as you develop NativeScript apps by catching silly errors and possible type mismatches at build time that could otherwise result in nasty app crashes later. Something plain old JavaScript can’t do (yet).
TypeScript has amazing module support. Use CommonJS modules, or even the new ES6 module spec. It all “just works” with TypeScript and NativeScript. It’s easier than the web. Really.
NativeScript CLI configures your .tsconfig files and transpiles your .ts files for you without any extra build configuration required. Just build your project and NativeScript CLI will handle the rest.
Some developers find TypeScript intimidating. Did you know you can just add a .ts extension to a JavaScript file and it will be processed like TypeScript? This can be very helpful as you migrate from a JavaScript style to TypeScript style.
You can find an easy to follow guide on how to migrate from JavaScript to TypeScript in this section of the TypeScript Deep Dive GitBook:
If you aren't sure what a certain block of TypeScript code does, you can paste the TypeScript code in the left hand box on TypeScript Playground. The generated JavaScript will be shown in the right hand box.