| Start |
|---|
Installation and setup BackgroundTypeScript is a typed superset of JavaScript that compiles directly to JavaScript code. TypeScript files commonly use the IDEsVisual Studio
Visual Studio Code
WebStorm
IntelliJ IDEA
Atom & atom-typescript
Sublime Text
Installing the command line interfaceInstall Node.js (opens new window)Install the npm package globallyYou can install TypeScript globally to have access to it from any directory. or Install the npm package locallyYou can install TypeScript locally and save to package.json to restrict to a directory. Installation channelsYou can install from:
Compiling TypeScript codeThe This creates a Compile using tsconfig.jsonYou can also provide compilation options that travel with your code via a With a |
| Basic syntax TypeScript is a typed superset of JavaScript, which means that all JavaScript code is valid TypeScript code. TypeScript adds a lot of new features on top of that. TypeScript makes JavaScript more like a strongly-typed, object-oriented language akin to C# and Java. This means that TypeScript code tends to be easier to use for large projects and that code tends to be easier to understand and maintain. The strong typing also means that the language can (and is) precompiled and that variables cannot be assigned values that are out of their declared range. For instance, when a TypeScript variable is declared as a number, you cannot assign a text value to it. This strong typing and object orientation makes TypeScript easier to debug and maintain, and those were two of the weakest points of standard JavaScript. Type declarationsYou can add type declarations to variables, function parameters and function return types.
The type is written after a colon following the variable name, like this:
The basic types are :
CastingYou can perform explicit casting through angle brackets, for instance: This example shows a Another way of casting in Typescript is using the Since Typescript 1.6, the default is using the ClassesClasses can be defined and used in TypeScript code. To learn more about classes, see the Classes documentation page (opens new window). |
Hello World Here we have a class, |
| TypeScript REPL in Node.js For use TypeScript REPL in Node.js you can use tsun package (opens new window) Install it globally with and run in your terminal or command prompt with Usage example: |
| Running TypeScript using ts-node ts-node (opens new window) is an npm package which allows the user to run typescript files directly, without the need for precompilation using Install ts-node globally using ts-node does not bundle typescript compiler, so you might need to install it. Executing script To execute a script named main.ts, run Running REPL To run REPL run command Example usage To exit REPL use command RemarksTypeScript aims to be a superset of JavaScript that transpiles to JavaScript. By generating ECMAScript compliant code, TypeScript can introduce new language features while retaining compatibility with existing JavaScript engines. ES3, ES5 and ES6 are currently supported targets. Optional types are a primary feature. Types allow static checking with the goal of finding errors early and can enhance tooling with features like code refactoring. TypeScript is an open source and cross platform programming language developed by Microsoft. The source code is available on GitHub (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference