- Basic Types
- Union Types
- Intersection Types
- Type Aliases
- Literal Types
- Nullable Types
- Type Assertions
- Function Types
- Tuple Types
- Mapped Types
- Types Vs Interfaces
Demonstrates the basic types available in TypeScript.
let employeeName: string = "Jim Halpert";
let age: number = 32;
let isManager: boolean = false;
const employee: { name: string; age: number; isManager: boolean } = {
name: "Pam Beesly",
age: 30,
isManager: false
};
Allows a variable to be one of several types.
let managerOrEmployee: string | boolean;
managerOrEmployee = "Michael Scott";
managerOrEmployee = true;
Combines multiple types into one.
type PersonalInfo = {
name: string;
age: number;
};
type JobInfo = {
title: string;
department: string;
};
type Employee = PersonalInfo & JobInfo;
const stanley: Employee = {
name: "Stanley Hudson",
age: 45,
title: "Sales Representative",
department: "Sales"
};
Creates a new name for a type.
type EmployeeName = string;
type EmployeeAge = number;
const angela: { name: EmployeeName; age: EmployeeAge } = {
name: "Angela Martin",
age: 37
};
Specifies exact values a variable can hold.
type Department = "HR" | "Sales" | "Accounting";
const kevin: { name: string; department: Department } = {
name: "Kevin Malone",
department: "Accounting"
};
Allows a type to also be null
or undefined
.
let creedNotes: string | null = "Top-secret document";
creedNotes = null;
Forces a variable to be treated as a specific type.
let customerFeedback: any = "Great service!";
let feedbackLength: number = (customerFeedback as string).length;
Describes types for functions, including parameters and return types.
type Greet = (name: string) => string;
const greetEmployee: Greet = (name: string) => {
return `Hello, ${name}`;
};
Defines an array with fixed types and length.
let dwightInfo: [string, number, boolean] = ["Dwight Schrute", 38, true];
Creates new types by transforming existing ones.
type Role = "admin" | "editor" | "viewer";
type Permissions = {
[role in Role]: boolean;
};
const rolePermissions: Permissions = {
admin: true,
editor: true,
viewer: false
};
Types and interfaces in TypeScript serve different purposes and have distinct use cases. Interfaces are primarily used to define the shape of objects or classes and support declaration merging, making them ideal for object-oriented programming and extending third-party libraries. Types, on the other hand, are more versatile, capable of representing primitives, unions, intersections, and complex type constructions. They are suitable for creating complex types, utility types, and working with a broader range of type definitions. Use interfaces for defining object structures and when declaration merging is needed, and use types for complex type definitions, working with primitives, and creating utility types.
-
Object Shape Definition:
- Use interfaces when defining the structure of an object or a class. Interfaces are specifically designed for this purpose and are more readable and conventional in the context of object-oriented programming.
-
Declaration Merging:
- Interfaces can be merged, meaning you can declare the same interface multiple times, and TypeScript will combine them into a single interface definition. This is useful for extending the capabilities of third-party libraries or organizing your code.
-
Complex Type Definitions:
- Use types when you need to define more complex types that go beyond the shape of an object. This includes union types, intersection types, and tuples.
-
Working with Primitives and Non-Object Types:
- Types are more versatile than interfaces as they can represent any kind of type, including primitives (string, number, boolean), unions, and intersections. This makes them suitable for a broader range of type definitions.
-
Utility Types and Mapped Types:
- Use types when working with utility types or mapped types to transform existing types into new ones.
This Gist was created with the assistance of ChatGPT.