DEV Community

Cover image for Five basic TypeScript Fundamentals you must know?
Ateeq Syed
Ateeq Syed

Posted on

Five basic TypeScript Fundamentals you must know?

We all loved understanding and writing code for Object Oriented Programming and Data Structures course but people who opt to write vanilla JavaScript tend to miss the concepts data types, classes, static type and null checking even if you did that course in Java or C++, but worry not, TypeScript is here to programmers who love writing low level programming code, let me introduce you to TypeScript

TypeScript is a popular programming language that is a superset of JavaScript. It adds several features to JavaScript, such as static typing and class-based object-oriented programming and here are five basic fundamentals you must know about TypeScript to get started and write low level code that you love:

Static Typing:

Static typing is the core feature of TypeScript that sets it apart from JavaScript. In JavaScript, variables can hold any type of data, and the data type can change at runtime. However, in TypeScript, variables have a static data type that is defined during development. This helps catch errors at compile time rather than at runtime, making code more robust.

For Example:

if a variable is defined as a string, it cannot be assigned a number value without throwing an error. This helps developers catch type errors earlier in the development process, making code more stable and reliable.

Object-Oriented Programming:

TypeScript supports class-based object-oriented programming. This allows developers to use features like inheritance and polymorphism that are not available in JavaScript. This makes it easier to write complex applications with a large number of objects and classes.

For Example:

Image description

Interfaces:

Interfaces are a key feature of TypeScript that allows developers to define the shape of objects. An interface defines the structure of an object, including the properties and methods it should have. This helps ensure that objects are used correctly and reduces the likelihood of runtime errors.

For Example:

An interface can define the shape of an object representing a user, including properties such as name, email, and age. Any object that implements this interface must have these properties, ensuring that the object is used correctly throughout the application.

Type Inference:

TypeScript has a powerful type inference system that allows the compiler to determine the type of a variable automatically. This means that developers do not need to explicitly define the type of every variable they create, making code faster to write and easier to read.

For Example:

Image description

Strict Null Checking:

TypeScript has a strict null checking feature that helps catch null and undefined errors at compile time. This is achieved by adding a question mark to the end of a variable's type definition, indicating that it can be null or undefined. This ensures that developers must explicitly handle null and undefined values, reducing the likelihood of runtime errors.

For Example:

in JavaScript, a function may return a null value, and if this value is not handled correctly, it can lead to errors. In TypeScript, strict null checking ensures that developers must handle null and undefined values correctly, making code more reliable.

Conclusion:

TypeScript is a powerful programming language that adds features like static typing, object-oriented programming, interfaces, type inference, and strict null checking to JavaScript. These features help make code more robust, reliable, and easier to maintain, making TypeScript an excellent choice for large-scale applications. By using TypeScript, developers can catch errors early in the development process, ensuring that their code is stable and reliable.

Top comments (0)