Typescript Basics
Nowadays, there are so many JavaScript frameworks (Angular, React) written and used to develop frontend UI by Typescript. It very important to every developer has very basic knowledge about Typescript. So, in this article we will look into Typescript Basics for getting started with it.
Pre-requisite: Basic knowledge of JavaScript, OOPs concept.
What is Typescript?
The Typescript language is a typed super set of JavaScript, means any program written using Typescript Language will be compiled into plain JavaScript.
What does Typescript solve over JavaScript? And Why Typescript Language is so much popular among Developers?
- Types & Equality
- Scope
- OOPs Implementation
- Module management
Installing Typescript:
There are two basic ways to install Typescript:
For via Npm, enter this command in command prompt:
The result will be helloworld.js file. It will return error if invalid type assignments or any.
Typescript Variables- Typescript variables must follow the JavaScript naming rules.
Types
In JavaScript, each variable has a type, but it changed on each assignment. String variable to a number, an object, or even a function. The development tool keeps guess type of a variable.
Typescript is optionally statically typed; this means that types are checked automatically to prevent accidental assignments of invalid values.
Operators
All the standard JavaScript operators are available within our Typescript program.
Functions
Let’s see several ways of write function in Typescript:
Code Organization:
Interfaces
Classes
Classes are structural element that helpful to organize function & variable within Typescript. It can be implemented as class-based object oriented programming. Let’s see simple example of class:
Access Modifiers
In this article, we have learned very basic concepts of typescript. I hope that it will be helpful to everyone when getting started with Typescript.
There are two basic ways to install Typescript:
- Via NPM (Nodejs Package Manager)
- Typescript’s Visual Studio plugins.
For via Npm, enter this command in command prompt:
- npm install -g typescript
save file with name as a .ts extension. Then compile it:
- tsc helloworld.ts
Typescript Variables- Typescript variables must follow the JavaScript naming rules.
Types
In JavaScript, each variable has a type, but it changed on each assignment. String variable to a number, an object, or even a function. The development tool keeps guess type of a variable.
Typescript is optionally statically typed; this means that types are checked automatically to prevent accidental assignments of invalid values.
- //string - a sequence of UTF-16 code units
- const name: string = 'Jayesh';
- // boolean - true or false
- const active: boolean = 100;
- //number - a double-precision 64-bit floating point value
- const score: number = 100;
- // array type
- const names: string[] = ['Jayesh', 'Arvind', 'Deep', 'Sanjay'];
- // object type
- let student: { name: string; score: number; };
- // Implementation of a student object
- student = { name: 'Jay', score: 99 };
- // enumerations type
- enum days { Saturday = 1,Sunday }
- const day = days. Saturday;
- // Tuple Types
- let accounts: [number , boolean , string];
- // Tuple Types - OK
- accounts = [1, true, 'Saving'];
- // Tuple Types - Error: 'string' is not assignable to 'number'
- accounts = ['my', true, 'Saving'];
All the standard JavaScript operators are available within our Typescript program.
Functions
Let’s see several ways of write function in Typescript:
- // function with parameter type & return type
- let getHello(name: string): string{
- return 'Hello ' + name;
- };
- // void - if function isn’t return any values.
- let printHello(name: string): string{
- console.log('Hello ' + name);
- };
- // default parameters
- let printHello(name: string, score=90): string{
- console.log ('Hello ' + name + ' Score ' + score);
- };
- // arrow functions
- const sum = (a: number, b: number) => { return a + b; }
Interfaces are not only used as abstract type that can be implemented by concrete classes. It will be also used to define any structure (defining the contracts) in our typescript. Let’s see Interfaces annotation as below:
- interface customer {
- // Properties
- name: string;
- // Methods
- printName(): void;
- }
Classes are structural element that helpful to organize function & variable within Typescript. It can be implemented as class-based object oriented programming. Let’s see simple example of class:
- class Student {
- // parameterize constructor
- constructor(private name: string, private rollNo: string) {
- }
- // function - default public
- print() {
- console.log('Student Name: ' + this.name + ' RollNumber: ' + this.rollNo);
- }
- }
- class Attendence {
- constructor(private students: Student[]) {
- }
- // function - default public
- getAttendance() {
- const student = this.getStudent();
- student.print();
- }
- // private function
- private getStudent() {
- const studentCount = this.students.length;
- const studentIndex = Math.floor(Math.random() * studentCount);
- return this.students[studentIndex];
- }
- }
- const students = [
- new Student('Jayesh', 1),
- new Student('Arvind', 2),
- new Student('Deep', 3),
- new Student('Sanjay', 4)
- ];
- const attendance = new Attendence(students);
- attendance.getAttendance();
- private - The private modifier restricts the visibility to the same-class only
- protected - The protected modifier allows the member to be used within the same-class, and within sub-classes. Access from anywhere else is not allowed.
- Public - which is the default for class members, allows access from all locations
Conclusion:
In this article, we have learned very basic concepts of typescript. I hope that it will be helpful to everyone when getting started with Typescript.
Comments
Post a Comment