Lab13 TypeScript
Lab13 TypeScript
console.log(`Explicit Types:`);
console.log(`Name: ${userName}, Type: ${typeof userName}`);
console.log(`Age: ${age}, Type: ${typeof age}`);
console.log(`Is Student: ${isStudent}, Type: ${typeof isStudent}`);
console.log('--------------------');
// 3. Type Inference
let country = "Canada"; // TypeScript infers 'string'
let pi = 3.14159; // TypeScript infers 'number'
let isActive = false; // TypeScript infers 'boolean'
console.log(`Inferred Types:`);
console.log(`Country: ${country}, Type: ${typeof country}`);
console.log(`Pi: ${pi}, Type: ${typeof pi}`);
console.log(`Is Active: ${isActive}, Type: ${typeof isActive}`);
console.log('--------------------');
console.log('--------------------');
// To run this:
// 1. npx tsc src/activity1.ts
// 2. node dist/activity1.js
5. Add console.log() statements to display your product objects and test your
functions.
6. Compile and run src/activity2.ts.
Solution:
// src/activity2.ts
console.log("Products:");
console.log(product1);
console.log(product2);
console.log('--------------------');
currentOrder = "shipped";
console.log(`Updated Order Status: ${currentOrder}`);
console.log('--------------------');
// To run this:
// 1. npx tsc src/activity2.ts
// 2. node dist/activity2.js
getCarInfo(): string {
return `${this.make} ${this.model} (${this.year})`;
}
}
// To run this:
// 1. npx tsc src/activity3.ts
// 2. node dist/activity3.js
// 1. Union Types
let id: number | string = 123;
console.log(`ID (number): ${id}`);
id = "abc-456";
console.log(`ID (string): ${id}`);
printId(100.123);
printId("my_unique_id");
// printId(true); // Error: Argument of type 'boolean' is not assignable to
parameter of type 'string | number'.
console.log('--------------------');
// 2. Literal Types
let direction: "up" | "down" | "left" | "right";
direction = "up";
console.log(`Direction: ${direction}`);
console.log('--------------------');
// Numeric Enum
enum UserRole {
ADMIN, // 0 by default
EDITOR, // 1
VIEWER = 5, // Explicit value, next will be 6
GUEST // 6
}
// String Enum
enum HttpMethod {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE"
}
console.log('--------------------');
// To run this:
// 1. npx tsc src/activity4.ts
// 2. node dist/activity4.js
Activity 5: Generics
Goal: Understand how to use generics to create reusable components and
functions that work with a variety of types while maintaining type safety.
Instructions:
1. Create a new file src/activity5.ts.
2. Generic Identity Function:
o Create a generic function identity<T>(arg: T): T that simply returns
the argument it receives.
o Call this function with a number, a string, and an object. Observe
how TypeScript correctly infers the type T for each call.
3. Generic Array Utility Function:
o Create a generic function getFirstElement<T>(arr: T[]): T |
undefined that takes an array of any type T and returns its first
element, or undefined if the array is empty.
o Test this function with an array of numbers, an array of strings, and
an array of objects.
4. Generic Interface:
o Define a generic interface Box<T> with a single property value: T.
interface User {
id: number;
name: string;
}
let users: User[] = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }];
let firstUser = getFirstElement(users); // T is inferred as User
console.log(`First user:`, firstUser);
// 3. Generic Interface
interface Box<T> {
value: T;
}
console.log('--------------------');
// To run this:
// 1. npx tsc src/activity5.ts
// 2. node dist/activity5.js
// 2. Type Guards
class Cat {
meow(): void {
console.log("Meow!");
}
}
interface Boat {
sail(): void;
}
startVehicle(myCarInstance);
startVehicle(myBoatInstance);
console.log('--------------------');
// To run this:
// 1. npx tsc src/activity6.ts
// 2. node dist/activity6.js
o Add at least 5-7 diverse items using addItem. Include items from
different categories and some with descriptions.
o Attempt to add an item with a duplicate ID.