2022-11-20

TypeScript

What is TypeScript

TypeScript is a statically-typed superset of JavaScript that was introduced by Microsoft. It was created to address the growing complexity of large-scale JavaScript applications. TypeScript extends JavaScript by introducing types, interfaces, decorators, and other features that make it easier to develop and maintain complex software systems.

While TypeScript includes all JavaScript functionalities, it offers additional features not present in JavaScript. The most important difference is TypeScript's static type system. Unlike JavaScript, which is dynamically typed, TypeScript allows you to specify the types of variables, function parameters, and function return values. This can help catch common bugs and make your code easier to understand and maintain.

TypeScript's Unique Features

TypeScript introduces a number of features that make it a powerful tool for writing complex applications. Let's take a deeper look at some of these key features.

Static Typing

The most fundamental feature of TypeScript is its static typing. Unlike JavaScript, which allows variables to change types freely (dynamic typing), TypeScript requires you to declare a variable's type when you define it. This means the type of a variable is checked before the code is run (at compile-time), helping to avoid potential type-related bugs.

ts
let message: string;
message = 'Hello, World!'; // Okay
message = 42; // Error: Type 'number' is not assignable to type 'string'.

Advanced Type Checking

TypeScript enhances JavaScript with sophisticated type-checking capabilities. This includes union types (a value that can be one of several types), intersection types (a value that must fulfill all of several types), and literal types (a value that must be exactly one specified value), among others.

ts
type StringOrNumber = string | number; // Union type

let variable: StringOrNumber;
variable = 'Hello'; // Okay
variable = 42; // Okay
variable = true; // Error: Type 'boolean' is not assignable to type 'StringOrNumber'.

Generics

Generics provide a way to make components work with any data type and not restrict to one data type. Generics provide a way to create reusable components. This is a powerful feature that gives you the flexibility of any type, while still enforcing type safety.

ts
function identity<T>(arg: T): T {
    return arg;
}

let output = identity<string>("myString");  // type of output will be 'string'

Interfaces

Interfaces in TypeScript are used to define a contract (or shape) for complex types. Once an interface is declared, any object that conforms to the structure of this interface can be treated as an instance of that interface.

ts
interface Person {
    name: string;
    age: number;
}

function greet(person: Person) {
    return `Hello, ${person.name}`;
}

greet({ name: 'Alice', age: 25 }); // Okay
greet({ name: 'Bob' }); // Error: Property 'age' is missing in type '{ name: string; }' but required in type 'Person'.

Enums

Enums or Enumerations are a new data type supported in TypeScript. They are used to create a collection of related values that can be numeric or string values.

ts
enum Color {
    Red,
    Green,
    Blue,
}

let c: Color = Color.Green;

References

https://www.typescriptlang.org/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!