1. WebsitePlanet
  2. >
  3. Glossary
  4. >
  5. Website builders
  6. >
  7. What is TypeScript?

What is TypeScript?

Miguel Amado Written by:
Christine Hoang Reviewed by: Christine Hoang
01 January 2025
TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It was developed and is maintained by Microsoft. TypeScript adds optional typing, classes, and other features, allowing developers to create more robust and maintainable code.

Definition of TypeScript

Technically, TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. This means that any valid JavaScript code is also valid TypeScript code. TypeScript builds upon the syntax and semantics of JavaScript while adding new features, such as static typing, classes, interfaces, and modules.

TypeScript’s primary aim is to help catch mistakes early through type checking and provide a more structured development experience. Unlike JavaScript, where types are determined at runtime (dynamic typing), TypeScript checks types at compile time (static typing). This allows developers to catch potential bugs before the code even runs.

Here’s a simple example that demonstrates the difference between JavaScript and TypeScript:

JavaScript:

function add(a, b) {
return a + b;
}

console.log(add(1, 2)); // 3
console.log(add(“1”, “2”)); // “12”

TypeScript:

function add(a: number, b: number): number {
return a + b;
}

console.log(add(1, 2)); // 3
console.log(add(“1”, “2”)); // Error: Argument of type ‘string’ is not assignable to parameter of type ‘number’.

In the TypeScript version, the add function is explicitly typed to accept two numbers and return a number. Trying to pass strings will result in a compilation error. This helps prevent unexpected behavior at runtime.

How Does TypeScript Work?

TypeScript code goes through a compilation process to convert it into JavaScript that can run in any JavaScript environment (browser, Node.js, etc.). This process involves several steps:

  1. TypeScript Code: You write code using TypeScript syntax, which includes types and other TypeScript-specific features.
  2. TypeScript Compiler: You run the TypeScript compiler (tsc), which reads your TypeScript code.
  3. Type Checking: The compiler performs static type checking based on the type annotations you’ve provided. If it finds any type-related issues, it reports them as compilation errors.
  4. Compilation to JavaScript: If there are no type errors, the compiler removes the type annotations and compiles the TypeScript code into plain JavaScript.
  5. JavaScript Output: The resulting JavaScript code can then be run in any environment that supports JavaScript, such as a web browser or Node.js.
Here’s an example of this process:

TypeScript Code (script.ts):
let message: string = “Hello, TypeScript!”;
console.log(message);

Compile with tsc:
tsc script.ts

Generated JavaScript Code (script.js):
var message = “Hello, TypeScript!”;
console.log(message);

The key advantage of this process is that type errors are caught during compilation, before the code is run. This early error detection can save significant debugging time.

Key Features of TypeScript

TypeScript offers several key features that enhance JavaScript development:

  1. Static Typing: TypeScript introduces static typing, which means variables, function parameters, and return values can be given a specific type. This allows for type checking at compile time, catching potential errors early.
  2. Type Inference: While TypeScript allows explicit type annotations, it also has a powerful type inference system. If you don’t explicitly specify a type, TypeScript will try to infer the most appropriate type based on the value assigned to the variable.
  3. Interfaces: Interfaces define the structure of an object, specifying the types of its properties and methods. They provide a way to define contracts within your code and are a powerful tool for creating reusable, modular code.
  4. Classes: TypeScript introduces class-based object-oriented programming to JavaScript. Classes in TypeScript support inheritance, modifiers (public, private, protected), and static properties/methods.
  5. Modules: TypeScript supports modules for organizing and encapsulating related code. It provides better code reusability and maintainability, especially in large-scale applications.
  6. Enums: Enums allow you to define a set of named constants. They can make your code more readable and less error-prone.
  7. Generics: TypeScript supports generic programming, allowing you to write reusable components that can work over a variety of types rather than a single one.
  8. Decorators: Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members. They can be used for things like adding metadata, modifying class behavior, dependency injection, etc.
  9. Compiler Options: TypeScript provides a wide range of compiler options that allow you to control how your TypeScript code is compiled into JavaScript, such as targeting a specific version of ECMAScript, generating source maps, etc.
These features, among others, make TypeScript a powerful language for developing large-scale JavaScript applications.

TypeScript and Object-Oriented Programming

One of the major advantages of TypeScript is its support for object-oriented programming (OOP). While JavaScript has some OOP capabilities, TypeScript extends this significantly with the introduction of classes, interfaces, and modifiers.

Classes: Classes in TypeScript are a way to define the blueprint for creating objects. A class can have properties (data) and methods (functions). Here’s an example:

class Person {
private name: string;

constructor(name: string) {
this.name = name;
}

public sayHello() {
console.log(`Hello, my name is ${this.name}.`);
}
}

let person = new Person(“Alice”);
person.sayHello(); // Output: Hello, my name is Alice.

In this example, Person is a class with a private property name and a public method sayHello. The constructor is a special method that is called when a new instance of the class is created.

Interfaces: Interfaces in TypeScript define a contract for the structure of an object. They specify the names and types of properties and methods that an object must have. Here’s an example:

interface Shape {
color: string;
area(): number;
}

class Circle implements Shape {
radius: number;
color: string;

constructor(radius: number, color: string) {
this.radius = radius;
this.color = color;
}

area(): number {
return Math.PI * this.radius ** 2;
}
}

In this example, Shape is an interface that requires any object conforming to it to have a color property of type string and an area method that returns a number. The Circle class implements this interface, meaning it must adhere to the structure defined by Shape.

Modifiers: TypeScript introduces access modifiers for class properties and methods. The three modifiers are:

  • public: The default. Public members can be accessed from anywhere.
  • private: Private members can only be accessed from within the class.
  • protected: Protected members can be accessed within the class and by classes derived from it.
These OOP features in TypeScript allow for the creation of more structured, maintainable, and scalable code, especially in large-scale applications.

TypeScript vs JavaScript

While TypeScript is a superset of JavaScript and any valid JavaScript code is also valid TypeScript code, there are significant differences:

  1. Static Typing: The most significant difference. TypeScript has static typing, meaning variables, function parameters, and return values can have specified types. JavaScript uses dynamic typing where types are determined at runtime.
  2. Early Error Detection: Because of static typing, TypeScript can catch type-related errors during compilation, before the code is run. With JavaScript, these errors would only be discovered at runtime.
  3. OOP Features: TypeScript introduces several object-oriented features that are not natively present in JavaScript, such as interfaces, enums, and access modifiers (public, private, protected).
  4. Compilation: TypeScript code needs to be compiled into JavaScript before it can be run. JavaScript code can be run directly without compilation.
  5. IDE Support: Because of its static typing and other features, TypeScript has better IDE support with features like auto-completion, navigation, and refactoring. This can significantly improve developer productivity.
Despite these differences, TypeScript is still fundamentally JavaScript. All TypeScript code compiles down to plain JavaScript that can be run in any JavaScript environment. TypeScript simply adds additional features on top of JavaScript to make the development process more robust and scalable.

TypeScript in the Real World

TypeScript has seen significant adoption in the real world, particularly in large-scale JavaScript projects. Many popular JavaScript libraries and frameworks either use TypeScript directly or provide TypeScript type definitions.

Some notable examples include:

  1. Angular: Angular, a popular front-end framework developed by Google, is written in TypeScript.
  2. Vue.js: While Vue.js is not written in TypeScript, it provides official TypeScript type definitions, making it easy to use with TypeScript.
  3. React: React, a library for building user interfaces, is not written in TypeScript but is commonly used with it. Create React App, a popular toolchain for React, supports TypeScript out of the box.
  4. Node.js: Many Node.js projects use TypeScript to add type safety to server-side JavaScript code.
  5. Deno: Deno, a secure runtime for JavaScript and TypeScript, uses TypeScript as a first-class language.
Many companies, including Microsoft, Google, Airbnb, Slack, and Asana, use TypeScript in their JavaScript projects. The adoption of TypeScript has been growing steadily, and it’s now one of the most popular languages on GitHub.

Getting Started with TypeScript

To start using TypeScript, you need to set up a TypeScript development environment:

  1. Install Node.js: TypeScript requires Node.js to be installed on your machine. You can download it from the official Node.js website.
  2. Install TypeScript: Once you have Node.js installed, you can install TypeScript globally using npm (Node Package Manager): npm install -g typescript
  3. Create a TypeScript file: Create a new file with a .ts extension, for example, hello.ts, and write some
  4. TypeScript code:
    let message: string = “Hello, TypeScript!”;
    console.log(message);
  5. Compile the TypeScript file: Use the TypeScript compiler to compile your .ts file into JavaScript:
    tsc hello.ts This will create a new file, hello.js, containing the compiled JavaScript code.
  6. Run the JavaScript file: You can now run the compiled JavaScript file using Node.js:
    node hello.js . This will execute your code and output the result.
There are also many IDEs and text editors that have excellent support for TypeScript, such as Visual Studio Code, which provides features like auto-completion, refactoring, and debugging out of the box.

TypeScript in Web Development

TypeScript is particularly useful in web development, where JavaScript is the primary language used for front-end development.

In a typical web development setup with TypeScript, you would:

  1. Write your front-end code in TypeScript: This could include your application logic, DOM manipulation, event handling, etc.
  2. Compile your TypeScript code into JavaScript: Use the TypeScript compiler to convert your TypeScript code into JavaScript that can run in a web browser.
  3. Bundle your compiled JavaScript: Use a bundler tool like Webpack, Rollup, or Parcel to bundle your compiled JavaScript files into a single file (or a few files) that can be included in your web pages.
  4. Include the bundled JavaScript in your HTML: Add a <script> tag in your HTML file to include the bundled JavaScript.
Many modern web development frameworks and libraries, such as Angular and React, have built-in support for TypeScript or can be easily used with TypeScript. This makes it straightforward to integrate TypeScript into your web development workflow.

Using TypeScript in web development can help you catch errors earlier, write more maintainable code, and take advantage of features like auto-completion and refactoring in your IDE. It’s particularly beneficial in large-scale web applications where the codebase is complex and is worked on by multiple developers.

TypeScript Best Practices

When working with TypeScript, there are several best practices that can help you write cleaner, more maintainable code:

  1. Use Type Annotations: While TypeScript can infer types in many cases, it’s often beneficial to explicitly annotate types for function parameters, return values, and variables. This makes your code more readable and can catch potential issues.
  2. Use Interfaces to Define Complex Types: If you have an object type that’s used in multiple places, define an interface for it. This makes your code more readable and allows you to reuse the type definition.
  3. Use const and let instead of var: TypeScript supports the const and let keywords introduced in ES6. Use const for values that shouldn’t be reassigned and let for values that may change. Avoid using var.
  4. Use ES6 Features: TypeScript supports many ES6 features like arrow functions, template literals, and destructuring. Use these features to write cleaner, more concise code.
  5. Use Modules: Organize your code into modules. Each module should have a single responsibility. This makes your code more maintainable and easier to test.
  6. Use Access Modifiers: Use private for class members that shouldn’t be accessible outside the class, protected for members that should be accessible in derived classes, and public for members that can be accessed from anywhere.
  7. Use readonly for Properties That Shouldn’t be Modified: If you have a property that should only be set in the constructor and never modified, mark it as readonly. This prevents accidental modifications.
  8. Use Type Guards: When working with union types, use type guards to narrow down the type before performing operations specific to that type.
  9. Enable Strict Type Checking: In your tsconfig.json file, enable strict type checking options like strict, noImplicitAny, strictNullChecks, etc. This will provide better type safety.
  10. Use a Linter: Use a linter like ESLint with TypeScript support to enforce consistent code style and catch potential issues.
Remember, the goal is to leverage TypeScript’s features to write code that is robust, maintainable, and expressive.

Summary

TypeScript is a statically typed superset of JavaScript that adds optional typing, classes, and other features to the language. Developed and maintained by Microsoft, it aims to make JavaScript development more robust and scalable, especially for large and complex projects.

The key advantage of TypeScript is that it allows developers to catch potential bugs and errors at compile time, before the code is run. This is achieved through static typing, where variables, function parameters, and return values can be given a specific type. TypeScript’s type inference system can also automatically infer types in many cases.

TypeScript is a powerful tool that can significantly enhance the JavaScript development experience. Its growing popularity and adoption in the industry make it a valuable skill for any JavaScript developer. Whether you’re working on a small project or a large-scale application, TypeScript can help you write better, more maintainable code.

Rate this Article
4.0 Voted by 2 users
You already voted! Undo
This field is required Maximal length of comment is equal 80000 chars Minimal length of comment is equal 10 chars
Related posts
Show more related posts
We check all user comments within 48 hours to make sure they are from real people like you. We're glad you found this article useful - we would appreciate it if you let more people know about it.
Popup final window
Share this blog post with friends and co-workers right now:
1 1 1

We check all comments within 48 hours to make sure they're from real users like you. In the meantime, you can share your comment with others to let more people know what you think.

Once a month you will receive interesting, insightful tips, tricks, and advice to improve your website performance and reach your digital marketing goals!

So happy you liked it!

Share it with your friends!

1 1 1

Or review us on 1

3505042
50
5000
114311764