1 of 10

What is TypeScript?

TypeScript is a superset of JavaScript that is compiled to JS, and has benefits like type safety. It helps catch errors during development.

​

Why use TypeScript?

  • Detects bugs at compile-time, not runtime.
  • Adds IDE auto-completion & better readability.
  • Supports OOP (Classes, Interfaces, etc.).

2 of 10

Basic Types

Just like in JavaScript, Typescript has primitive data types and non-primitive data types.

Primitive Types:

  • number: represents both integer and floating-point numbers.

​

​

​

​

  • string: represents textual data, enclosed in single quotes, double quotes, or backticks (for template literals).

​

​

​

​

  • boolean: represents a logical value, either true or false.

​

  • null: represents the intentional absence of any object value.
  • undefined: represents a variable that has been declared but not yet assigned a value, or a property that doesn't exist.
  • symbol: represents a unique, immutable value, often used as object keys.
  • bigint (ES2020+): represents integers with arbitrary precision, larger than what number can safely represent.

3 of 10

Basic Types

Primitive Types:

  • null: represents the intentional absence of any object value.

​

​

  • undefined: represents a variable that has been declared but not yet assigned a value, or a property that doesn't exist.

​

​

  • symbol: represents a unique, immutable value, often used as object keys.

​

​

​

  • bigint (ES2020+): represents integers with arbitrary precision, larger than what number can safely represent.

4 of 10

Basic Types

Object Types:

  • Array: represents a collection of elements of the same type.

​

​

​

  • Tuple: represents an array with a fixed number of elements whose types are known at specific positions.

​

​

  • enum: provides a way to define a set of named constant values.

​

​

​

  • any: represents any type, effectively opting out of type-checking for a variable. Use with caution.

See Labs for code demo

5 of 10

Basic Types

Object Types:

  • void: represents the absence of any type, typically used as the return type for functions that do not return a value.

​

​

​

  • never: represents the type of values that never occur, often used as the return type for functions that always throw an error or never return.

​

​

  • unknown: similar to any, but safer. You cannot perform operations on an unknown type without first narrowing it to a more specific type.

​

​

​

  • Object: represents any non-primitive type. While available, using more specific types like interfaces or classes is generally preferred.

See Labs for code demo

6 of 10

Type Aliases, Union Types, Intersection Types

  • Union types: a union type allows a value to be one of several types. Use | to combine types.

​

​

​

​

  • Intersection types: an intersection type combines multiple types into one, requiring the value to satisfy all constituent types. Use & to merge types.

​

​

​

​

  • Type aliases: type aliases allow you to create custom names for existing types. They're defined using the type keyword.

​

​

​

​

See Labs for code demo

7 of 10

Functions Typing and Interfaces

​

​

  • Function typing: you can define types for parameters and return values.

​

​

​

​

​

​

  • Interfaces: interfaces define the structure (shape) of objects.

​

​

​

​

​

​

​

​

See Labs for code demo

8 of 10

Classes and OOP Concepts

TypeScript enhances JavaScript classes with type annotations and access modifiers.

There are three main visibility modifiers in TypeScript:

  • public: (default) allows access to the class member from anywhere.
  • private: only allows access to the class member from within the class.
  • protected: allows access to the class member from itself and any classes that inherit it.

​

Keywords that support OOP concepts:

  • Extend: Classes can extend each other through the extends keyword. A class can only extend one other class.
  • Implement: Interfaces can be used to define the type a class must follow through the implements keyword.
  • Override: Classes can override a base class method through the override keyword.

See Labs for code demo

9 of 10

Generics

Generics allows developers to create reusable components that can work with a variety of data types while maintaining type safety

Generics can be implemented with functions, interfaces, etc.

See Labs for code demo

10 of 10

Modules

A module is just a file that contains reusable code like variables, functions, or classes, and can be imported or exported to other files.

​

There are two module systems in JavaScript:

  • CommonJS (CJS): used in older versions of Node.js (<13). File extension are .js or .cjs
  • ES Modules (ESM): Used in Modern JS (browser & Node >= v13). File extension are .mjs or "type": "module" in package.json

See Labs for code demo