TypeScript = JavaScript + powerful type system
- Overview of TypeScript
- Features of TypeScript
- Advantages and Disadvantages
- Installation of TypeScript
- Conclusion
Overview
of TypeScript
Before diving in, familiarise
yourself with TypeScript's fundamentals.
TypeScript is an open source
programming language was first released in 2012 by MicroSoft.
TypeScript is a superset of JavaScript
that means it was built on JavaScript. TypeScript has all the functionality of JavaScript
as well as some unique extra features. Such as type determination, interfaces,
abstract classes, access modifiers, static type verification, and so forth.
The primary purpose of TypeScript
is to carefully control the data type of variables during compilation and
coding to ensure error-free development. Additionally, it examines the data
type and syntax of the methods, classes, objects, and functions.
Unlike JavaScript, which
decides the data type of values at runtime.
TypeScript detects errors
during compilation so it is referred to be a static typing method whereas JavaScript
is a dynamically typing method, which means it detects error during run time.
Transpilation is the process by
which developers can import JavaScript programmes into TypeScript and compile
them using the TypeScript compiler on their IDE (Integrated Development Environment)
platform.
JavaScript source code cannot
be impacted by TS during compilation. Without modifying the source code, the TS
compiler simply determines the data types of variables, checks for errors, and
runs as JavaScript.
Features
of TypeScript with playground examples
1. TypeScript explicitly sets the data type of variables while coding whereas JavaScript doesn’t.
2. Using the value that has been assigned to the variable, TypeScript automatically determines the type of any functions or variables.
Here, data type of “sales” is not specified but
TypeScript auto detects the type of “sales” based on the value assigned to “sales”.
4. TypeScript's stringent type checking at both the coding and build time is a huge benefit. For greater coding flexibility, we can control the type checking strictness with the TypeScript compiler option "Strict".
A list of type checking rules is contained in strict mode. For severe type checking, simply enable this mode in the TypeScript compiler settings.
It supports third party library functions as well.
6. Type Assertion
TypeScript uses the type assertion technique to convert a
variable from one type to another.It is called type
narrowing and allows you to override its inferred and analyzed view of types in
any way you want to.
Here, employee code is globally annotated as "any," and it is converted to "number" type via type assertion to generate a gift coupon locally.
Using “as” keyword
Advantages of TypeScript
§
TypeScript strictly enforces type checking during compilation
and coding, allowing developers to write error-free code that can be safely
run.
§ It can be used in both the front and back end as JavaScript.
§ TypeScript
provides code completion, code refactoring, productivity boosting, and type
checking.
§ It
can be used in medium- to large-scale projects.
§ TypeScript
can be executed anywhere as JavaScript.
§ TypeScript
highlights unexpected behavior in code and reduces the number of threads.
§ It
is a type system; there is no need to add extra code to annotate types
explicitly.
§ TypeScript
includes abstract classes, access modifiers, and interfaces that are not
present in JavaScript.
Disadvantage of TypeScript
§ Too
strict type-checking can sometimes affect flexibility in coding.
§ Without
the assistance of JavaScript, TypeScript cannot be executed directly in
browsers.
§ Errors
are possible during runtime. because TypeScript does all of its type checking
during the build process.
How to install TypeScript and
configuring the TypeScript compiler?
Simply follow the steps!
Installation of TypeScript
1. We
have to install NPM for the TypeScript compiler, so go to https://nodejs.org/en/ and
download the latest version of Node.js.
2. Open a terminal on your computer.
3. Type the following commands to install TypeScript:
Here, "sudo" is needed when Mac or Linux show a permission error during installation.
4. Verify installation using the following command:
Configuring the TypeScript Compiler
Some
IDE platforms do not include the TypeScript compiler. For instance, the Visual
Studio Code editor does not include the TypeScript compiler, so we have to
configure the TS compiler for the VS Code editor. Here are the steps:
1. In your VS Code editor, create a
typescript project and save it with the “ts” extension.
2. Navigate to the TS project folder in terminal.
3. Enter the following command:
The terminal will look
like this:
Conclusion
To summarize, TypeScript is a type system that includes all of the features of JavaScript as well as strict type checking and static typing to create error-free code.
TypeScript = JavaScript + powerful type system




.png)
.png)








Comments
Post a Comment