Basic Concepts and Syntax of TypeScript

  • A TypeScript code comprise of variables, functions, statements, comments etc.
  • The identifiers, such as variables and function name, can include characters or digits or both. But they cannot begin with digits.
  • Identifiers cannot contain special symbols except underscore(_) or dollar sign($). We cannot use keywords as identifiers and they must be unique within a scope. They are case-sensitive and cannot contain spaces. Some examples of Identifiers are: firstName, last_Name, number1 etc.
  • TypeScript is case-sensitive that means it can differentiate between uppercase and lowercase. For example, name and Name are two different identifiers in TypeScript.
  • Each line of code is called a statement. Semicolon (;) are optional at the end of the statement.
  • If you include multiple statements in a single line, then those statements must be separated using semicolon.
  • There are two types f comments in TypeScript:
    • Single Line Comments(//): Any text between // and the end of the line is considered as comment and will not be executed.
    • Multiple Line Comments(/* */): Any text between /* and */ are considered as comment even if it spans multiple lines and will not be executed.

 

 

TypeScript Environment Setup

There are multiple ways to setup the environment for TypeScript.

I will be using Visual Studio Code to create applications using TypeScript.

Download and install Node.js as per your system configuration.

Open Node.js command prompt. Use it to install TypeScript.

If you get any error while using below command, verify whether you have installed Node.js crrectly or not. Use node -v command to get the version number of Node.js and to verify the successful installation of Node.js.

Download and install Visual Studio Code as per your system configuration.

Introduction to TypeScript

Introduction to TypeScript

  • TypeScript is a subset of JavaScript. TypeScript code is compiled into JavaScript code. TypeScript = JavaScript + Additional Features.
  • TypesScript files are saved with .ts. extension.
  • Any valid .js file can be renamed to .ts.
  • Compiled TypeScript can be consumed from any JavaScript code.
  • It is pure Object Oriented Language like C# or Java with classes, objects, interfaces, inheritance etc.
  • To learn TypeScript, you should have basic knowledge of JavaScript and Object Oriented concepts.
  • TypeScript is portable across browsers, devices and operating systems. It can run on any environment where JavaScript can run.
  • TypeScript provides with error-checking feature. It compiles the code and generate compilation error, if there are any syntax errors.
  • TypeScript is strongly typed script. Variable declared with no type can be inferred by TLS (TypeScript Language Service) based on its value.
  • TypeScript also supports  type definition for existing JavaScript libraries. TypeScript Definition file, .d.ts extension, provides definition for external JavaScript libraries.
  • When TypeScript is compiled, there is an option to create a declaration fie, which functions as an interface to the components in the compiled JavaScript.
  • Declaration files are similar to header files in C++.

Components of TypeScript

  • Language: It has syntax, keywords and type annotations.
  • TypeScript Compiler: TypeScript Compiler (TSC) converts the TypeScript code into JavaScript.
  • TypeScript Language Service: It exposes an additional layer around the core compiler pipeline that are editor-like applications.

 

 

Design a site like this with WordPress.com
Get started