Introduction
TypeScript, an open source language, is
a typed superset of JavaScript that compiles to plain JavaScript. It adds
optional static typing and class-based object oriented programming to the
language. Some of the key features it provides are type support and code
encapsulation.
Overview of the relation between
TypeScript and
JavaScript
As in the above figure,
MathCalculator.ts is a TypeScript file that is compiled by tsc, the TypeScript
compiler, to the MathCalculator.js file. Then one can use this MathCalculator.js
in their application.
OOPS
Features of TypeScript
-
Supports
Key JavaScript Features
-
Provides
static typing
-
Supports
constructors, properties and functions
-
Define
Interfaces
-
Encapsulation through classes and modules
-
Support
lambdas =>
TypeScript Annotations
Primitive
Types
Basic primitive types
are number, bool, string, null and undefined.
Object Types
Object literals
Functions
Here functions can be used in the
following two ways:
-
Assigning the
function directly
-
Defining a type as a
function and then assign a function
Arrow function expressions
Equivalent JavaScript code
Dynamic and Static typing
TypeScript |
JavaScript |
Static typing is
optional here |
Only dynamic
typing is supported |
Type safety is
checked at compile time |
Type safety is
checked at run time |
Summary
In this article, we have seen that
TypeScript *.ts files are compiled to *.js files by the TypeScript compiler (tsc).
Also we have seen annotations and various object types available in TypeScript.
In future articles, we will explore classes, interfaces and modules.