Vishal 03 JSV
Vishal 03 JSV
EXPERIMENT-03
AIM: Write TypeScript code demonstrating static typing and type
inference in JavaScript, including type annotations and interfaces.
Source-Code:
/**
* @type {boolean}
*/
let isActive = true;
/**
* Function to add two numbers
* @param {number} x
* @param {number} y
* @returns {number}
*/
Name-Vishal Kumar Pandey Roll no.-11222881 BCSE-703L (Advanced JavaScript Lab)
function add(x, y) {
return x + y;
}
/**
* Function to print person details
* @param {Person} person - The person object
*/
function printPerson(person) {
console.log(`Name: ${person.name}, Age: ${person.age}, Active:
${person.isActive}`);
}
printPerson(inferredPerson);
// Using union types in JavaScript (simulated)
/**
* @param {number|string} id - The ID can be either a number or a string
*/
function printId(id) {
if (typeof id === 'string') {
console.log(`ID is a string: ${id.toUpperCase()}`);
} else {
console.log(`ID is a number: ${id}`);
}
}
Name-Vishal Kumar Pandey Roll no.-11222881 BCSE-703L (Advanced JavaScript Lab)
OUTPUT: