GIAIC - 06-Advance TS Topics
GIAIC - 06-Advance TS Topics
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
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