TypeScript Advanced Concepts
WRITE ROBUST, CLEAN AND MORE EXPRESSIVE CODE IN
TYPESCRIPT
Some Advance TypeScript Concepts
• TypeScript that allow us to write more expressive and robust code. We
will cover types, interface, union and intersection types, type inference,
generics, and type guards.
Types Vs Interface
Types:
The type keyword in TypeScript allows us to define the shape of data.
It provides a way to create new names for existing types.
Type aliases don’t create new types; they provide alternative names for
existing ones.
Interface:
Interfaces define contracts that objects must adhere to.
They focus on naming types and expressing shape.
Used for describing object shapes and contracts.
Use Case for Types
Shorthand for primitive types (e.g., MyNumber as a shorthand for
number).
Representing complex data structures (e.g., User type).
// Creating type aliases
type MyNumber = number;
type UserType = {
id: number;
name: string;
email: string;
};
Use Case for Interfaces
Describing object properties and methods.
Enforcing contracts within code.
// Defining an interface
interface Client {
name: string;
address: string;
}
Difference and Similarities in Types and
Interfaces
Both types and interfaces can represent similar shapes.
Use interfaces for object shapes and contracts.
Use types for versatility (unions, intersections, etc.).
Fresh Vs Stale Objects In TypeScript
Freshness (also known as strict object literal checking) helps catch
excess or misspelled properties in object literals during type checks.
It ensures that object literals adhere to their defined shape.
A typecan include an index signature to explicitly permit excess
properties
Tuples?
Tuples:
A tuple is an ordered list of elements with fixed types.
It allows you to express an array where each element has a specific type.
Syntax: let myTuple: [string, number] = ["Hello", 42];
Inthe example above, myTuple is a tuple containing a string followed by a
number.
Tuples are useful when you need to represent a fixed-size collection of different
types.
Union?
Union Types:
Union types allow you to declare a type that can be one of several types.
Syntax: type MyType = string | number | boolean;
In the example above, MyType can be either a string, a number, or a boolean.
Useful for scenarios where a value can have multiple possible types (e.g., a
function parameter that accepts either a string or a number).
Intersection?
Intersection Types:
Intersection types combine multiple types into one.
Syntax: type CombinedType = TypeA & TypeB;
In the example above, CombinedType includes all properties and methods
from both TypeA and TypeB.
Useful when you want to create a type that has features from multiple existing
types.
GitHub Repositories to Follow
Panaverse TypeScript Repository by Sir Zia Khan
https://github.com/panaverse/learn-typescript.git
Learn GIT by Zeeshan Hanif
https://www.youtube.com/watch?v=MiXAma2db8Y&list=PLKueo-cldy_HjRnPUL4G3pWHS7FREAizF&index=2
TypeScripts Assignments
https://github.com/panaverse/learn-typescript/tree/master/NODE_PROJECTS
https://github.com/panaverse/learn-typescript/blob/master/NODE_PROJECTS/getting-started-exercises.md
Class Repository
https://github.com/fkhan79/giaic_fmk/