let bounty: number = 30_000_000;
let devilFruit: string = "Gomu Gomu no Mi";
let isCaptain: boolean = true;
let crew: string[] = ["Luffy", "Zoro", "Nami", "Sanji", "Usopp"];
let wantedPoster: [string, number]
wantedPoster = ["Luffy", 30_000_000]
console.log(`Wanted: ${wantedPoster[0]}`); // Wanted: Luffy
console.log(`Bounty: ${wantedPoster[1]} Berries`); // Bounty: 30000000 Berries
const crewMember: { name: string; age: number; role: string; ateDevilFruit: boolean } = {
name: "Monkey D Luffy",
age: 19,
role: "Captain",
ateDevilFruit: true
};
function setSail(destination: string): string {
return `The Straw Hat Crew is heading for ${destination}!`;
}
console.log(setSail("Wano")); // The Straw Hat Crew is heading for Wano!
async function eatFood(): Promise<string> {
return "Luffy ate all the food!";
}
let swordStyle: string = "Santoryu";
swordStyle = 3; // Error: Type 'number' is not assignable to type 'string'
function identifyOpponent(opponent: string | number) {
if (typeof opponent === "string") {
return `Fighting ${opponent}`;
} else {
return `Bounty: ${opponent} berries`;
}
}
identifyOpponent("Mr. 1"); // Fighting Mr. 1
identifyOpponent(75_000_000); // Bounty: 75000000 berries
let swordCount = 2; // TypeScript infers 'swordCount' as a number
swordCount = 3; // Zoro adds another sword
type FavouriteThing = "Tangerines";
const nami: FavouriteThing = "Tangerines";
interface PirateProfile {
crew: string;
role: string;
bounty?: number; // Optional property
}
interface PirateProfile {
crew: string;
role: string;
bounty?: number; // Optional property
}
interface NavigatorProfile extends PirateProfile = {
mapsCollected: number;
ownsLogPose?: boolean; // Optional property
}
const nami: NavigatorProfile = {
crew: "Straw Hat Pirates",
role: "Navigator",
bounty: 66000000,
mapsCollected: 50,
ownsLogPose: true
};
In short:
Use interfaces when defining objects, especially when they might need to be extended or merged.
Use type aliases for primitive types, complex types, unions, intersections, or when you're defining something that isn’t strictly an object.
Type aliases can define objects as well, but they don’t support extension or declaration merging.
interface
are available in type
, the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable."|
function longRangeAttack(attack: string | number) {
typeof attack === "string"
? console.log(`Usopp attacks with his ${attack}!`)
: console.log(`Usopp fires a shot from ${attack} meters away!`);
}
longRangeAttack("Kouzuki's Slingshot"); // Usopp attacks with his Kouzuki's Slingshot!
longRangeAttack(100); // Usopp fires a shot from 100 meters away!
let taskStatus: "Pending" | "In-progress" | "Completed" = "Pending";
taskStatus is a variable
The type of taskStatus is restricted to the exact values "Pending", "In-progress", or "Completed"
It is initially set to "Pending"
type Sniper {
attackRange: number;
observationHaki: boolean;
}
type Storyteller {
storiesTold: number;
}
type BraveWarrior {
braveryLevel: string;
}
type Usopp = Sniper & Storyteller & BraveWarrior;
const usoppProfile: Usopp = {
attackRange: 100,
observationHaki: true,
storiesTold: 9001,
braveryLevel: "low",
};
function myFunction<T>(param: T): T {
return param;
}
function cookDish<T>(dish: T): T {
console.log(`Cooking ${dish}`);
return dish;
}
const luffyMeal = cookDish<string>("Meat Skewers")
console.log(luffyMeal); // "Meat Skewers"
const namiMeal = cookDish<string>("Tropical Fruit Salad")
console.log(namiMeal); // "Tropical Fruit Salad"
2
let message: string = 'Hello World';
console.log(message);
3
tsc helloworld.ts
node helloworld.js