0% found this document useful (0 votes)
4 views4 pages

TypeScript Advanced Lab

The document is a lab manual for advanced TypeScript programming, containing 10 programs that cover various topics such as interfaces, generics, async/await, arrow functions, union types, tuple types, enums, type assertions, optional chaining, and readonly properties. Each section includes code examples and their corresponding outputs to illustrate the concepts. This manual serves as a practical guide for enhancing TypeScript skills.

Uploaded by

hitsaiml2c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

TypeScript Advanced Lab

The document is a lab manual for advanced TypeScript programming, containing 10 programs that cover various topics such as interfaces, generics, async/await, arrow functions, union types, tuple types, enums, type assertions, optional chaining, and readonly properties. Each section includes code examples and their corresponding outputs to illustrate the concepts. This manual serves as a practical guide for enhancing TypeScript skills.

Uploaded by

hitsaiml2c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

TypeScript Advanced Programming Lab

Lab Manual: 10 Advanced TypeScript Programs

Contents:

1. 1. Interface and Class


2. 2. Generics
3. 3. Async/Await
4. 4. Arrow Functions
5. 5. Union Types
6. 6. Tuple Type
7. 7. Enums
8. 8. Type Assertion
9. 9. Optional Chaining
10. 10. Readonly Properties

1. Interface and Class


Code:

interface Person {
name: string;
age: number;
}
class Student implements Person {
constructor(public name: string, public age: number) {}
display(): void {
console.log(`${this.name} is ${this.age} years old.`);
}
}
const s = new Student("Alice", 22);
s.display();

Output:

Alice is 22 years old.


2. Generics
Code:

function identity<T>(arg: T): T {


return arg;
}
console.log(identity<number>(42));

Output:

42

3. Async/Await
Code:

async function fetchData(): Promise<string> {


return "Data received";
}
fetchData().then(data => console.log(data));

Output:

Data received

4. Arrow Functions
Code:

let nums = [1, 2, 3];


let squares = nums.map(n => n * n);
console.log(squares);

Output:

[1, 4, 9]

5. Union Types
Code:
function printId(id: number | string) {
console.log(`ID: ${id}`);
}
printId(101);
printId("xyz");

Output:

ID: 101
ID: xyz

6. Tuple Type
Code:

let employee: [number, string] = [1, "John"];


console.log(employee);

Output:

[1, 'John']

7. Enums
Code:

enum Direction { Up, Down, Left, Right }


let dir: Direction = Direction.Up;
console.log(dir);

Output:

8. Type Assertion
Code:

let someValue: any = "this is a string";


let strLength: number = (someValue as string).length;
console.log(strLength);
Output:

16

9. Optional Chaining
Code:

let person = { name: "Jane", address: { city: "NY" } };


console.log(person?.address?.city);

Output:

NY

10. Readonly Properties


Code:

interface Point {
readonly x: number;
readonly y: number;
}
let p: Point = { x: 10, y: 20 };
console.log(p);

Output:

{ x: 10, y: 20 }

You might also like