Tiếng Việt

TypeScript for Beginners - Taking JavaScript Code to the Next Level

Bronie.png

Author

Huybu

06/12/2023

Share

typescript-logo.png

 

Welcome to the first article in the TypeScript learning series! In this article, we will explore TypeScript from the perspective of those who already know JavaScript. Start your journey to learn TypeScript and enjoy its benefits!

1. Introduction to TypeScript

TypeScript is a static programming language based on JavaScript. It helps us to write JavaScript code more efficiently, while providing more reliability and maintainability. TypeScript is developed by Microsoft and is widely used in large and complex projects.

2. Why use TypeScript?

Although JavaScript is a powerful programming language, it does not have static data types and time-checking for errors. compilation point. This can lead to an error during program execution. TypeScript solves this problem by adding a static data type and checking for errors at compile time. This helps you detect bugs earlier and increases the reliability of your code.

In addition, TypeScript provides smart suggestions, powerful code analysis, and a well-integrated IDE. This increases development productivity and reduces bug finding time.

3. Integrate TypeScript into an existing JavaScript project

If you already have an existing JavaScript project and want to switch to TypeScript, don't worry. TypeScript can be integrated into an existing JavaScript project easily. You can start by renaming the JavaScript code files to .ts and start adding data types to the code sections one by one. This allows you to take advantage of TypeScript step by step without rewriting the entire code.

4. Using data types in TypeScript

One ​​of the most important features of TypeScript is the ability to use static data types. You can define data types for variables, parameters, and function return values. This helps you to check and ensure the validity of the source code.

For example:

function add(a: number, b: number): number {
  return a + b;
}
let result: number = add(3, 5);

In the above example, we define the data type for the variable "a" and "b" as "number", and the data type of the return value is "number". TypeScript will check these data types and report an error if they don't match.

5. Type Contracts

TypeScript also supports type contracts, allowing you to define data type and interface constraints for objects. This helps to define required attributes and comply with specific data type rules.

For example:

interface Person {
  name: string;
  age: number;
}
function greet(person: Person): void {
  console.log("Hello, " + person.name + "!");
}
let john = { name: "John", age: 25 };
greet(john);

In the above example, we define an interface "Person" with two properties "name" (string type) and "age" (of type number). The "greet" function takes as an argument an object with a similar structure to the "Person" interface. This ensures that only objects that conform to the "Person" contract type can be passed into the "greet" function.

If we declare an additional attribute “gender” with value “male” to the object “join”  

let john = { name: "John", age: 25, gender: "male" };

Now the "john" object has an undeclared property in the type contracts "Person" as "gender" . TypeScript will display an error message:

Object literal may only specify known properties, and 'gender' does not exist in type 'Person'.

With TypeScript, compile-time data type checking helps you identify and correct errors right from development development, increasing the reliability and maintainability of the code.

6. Take advantage of TypeScript's advanced features

TypeScript has many other advanced features like:

Generics: Generics allows you to write flexible and reusable code for a variety of data types without the need for definitions. tough data type.

Module: TypeScript supports modules for managing code in large projects, helping to break code into separate parts and reuse them in other files.

Class and Inheritance: TypeScript supports full object-oriented programming with class and inheritance, allowing class definition , properties, methods, and use inheritance to share code between classes.

And more. 

These features, along with others in TypeScript, help you write more flexible and efficient code, while reducing Reduce bugs and increase code maintainability.

7. Conclude

The above is a brief introduction to TypeScript for those who already know JavaScript. TypeScript provides data type validation, smart hints, and powerful code analysis, helping you write more efficient and maintainable JavaScript code. Hope this article has helped you to have an overview of TypeScript and discover its potential. Keep learning and applying TypeScript to your projects to enjoy its benefits!


 

Share

Contact Us