Is TypeScript based on JavaScript?

Is TypeScript Based on JavaScript?

Introduction

TypeScript is a statically typed, object-oriented, multi-paradigm programming language developed by Microsoft as a superset of JavaScript. It is designed to support large-scale JavaScript applications, improve the maintainability, readability, and scalability of existing JavaScript codebases, and add optional static type checking and features like interop with C# and F#.

JavaScript Basics

Before diving into the relationship between TypeScript and JavaScript, let’s quickly review some JavaScript basics.

JavaScript is a dynamically-typed language, which means that the data type of a variable is determined at runtime. It is also a prototype-based language, where objects inherit properties and methods from other objects. JavaScript has two main syntax constructs: let, const, and var, which are used to declare variables. let and const are used for variable declarations, while var is used for variable declarations with scope scope-only.

Here’s a simple JavaScript example:

console.log("Hello World!"); // log 'Hello World!'
let x = 10; // declare a variable with scope-only
const y = 20; // declare a constant
var z = 30; // declare a variable with scope-exclusive

TypeScript Basics

Now, let’s introduce TypeScript basics.

TypeScript is a statically-typed language, which means that the data type of a variable is determined at compile-time. It has its own syntax for declaring variables, which is similar to JavaScript. However, it also has some additional features that make it a more powerful language.

Here’s a simple TypeScript example:

let x: number = 10; // declare a variable with scope-only
const y: number = 20; // declare a constant
var z: number = 30; // declare a variable with scope-exclusive

Inheritance and Polymorphism

TypeScript supports inheritance, which allows you to create a new class that inherits properties and methods from an existing class. This is similar to JavaScript’s prototype-based inheritance model.

Here’s a simple TypeScript example:

class Animal {
name: string;

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

speak(): string {
return `The animal makes a sound.`;
}
}

class Dog extends Animal {
speak(): string {
return "The dog barks.";
}
}

Interfaces and Type Inference

TypeScript supports interfaces, which define a contract for a class or function. This makes it easy to write reusable code and avoid type-related errors.

Here’s a simple TypeScript example:

interface Shape {
area(): number;
}

class Circle implements Shape {
private radius: number;

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

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

Functions

TypeScript supports functions, which are first-class citizens in the language. This means that you can call functions like variables, store them in arrays, and pass them as arguments to other functions.

Here’s a simple TypeScript example:

function greet(name: string): string {
return `Hello, ${name}!`;
}

console.log(greet("John")); // log 'Hello, John!'

Interfaces and Type Inference

Interfaces and type inference are closely related in TypeScript.

When you define an interface, TypeScript will infer the types of the properties based on the properties defined in the interface. For example:

interface Shape {
area(): number;
}

function getArea(shape: Shape): number {
return shape.area();
}

In this example, TypeScript will infer that the getArea function takes a Shape object as an argument and returns a number.

Modules and External Dependencies

TypeScript supports modules, which are isolated units of code that can be included or excluded based on the build process.

Here’s a simple TypeScript example:

// module.ts
interface Shape {
area(): number;
}

class Circle implements Shape {
private radius: number;

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

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

export { Circle };

// app.ts
import { Circle } from './module';

console.log(Circle.area());

In this example, the module file exports a Circle object, which can be included or excluded based on the build process.

Conclusion

TypeScript is a statically-typed, object-oriented programming language that is based on JavaScript. It supports inheritance, polymorphism, interfaces, and type inference, making it a powerful tool for building large-scale JavaScript applications. While it may seem like TypeScript is a complete superset of JavaScript, there are still some differences between the two languages. However, for developers already familiar with JavaScript, TypeScript can be a valuable addition to their toolkit.

List of Key Differences

  • TypeScript is a statically-typed language, while JavaScript is dynamically-typed.
  • TypeScript supports inheritance, while JavaScript does not.
  • TypeScript supports interfaces, while JavaScript does not.
  • TypeScript supports type inference, while JavaScript does not.
  • TypeScript has its own syntax for declaring variables, while JavaScript uses let, const, and var.

Table: TypeScript and JavaScript Comparison

Feature JavaScript TypeScript
Type Checking No built-in type checking Yes, with optional static type checking
Inheritance No inheritance Yes, with inheritance
Interfaces No interfaces Yes, with interfaces
Type Inference No type inference Yes, with type inference
Modules No modules Yes, with modules

I hope this article helps you understand the relationship between TypeScript and JavaScript.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top