typescript 2
10.2
Declare an interface named - Product with two properties like productId and productName with a number and string datatype and need to implement logic to populate the Product details.
interface Product {
productId: number;
productName: string;
}
function populateProductDetails(product: Product, id: number, name: string): void {
product.productId = id;
product.productName = name;
}
const newProduct: Product = { productId: 0, productName: '' };
populateProductDetails(newProduct, 123, 'Example Product');
console.log(newProduct); // Output: { productId: 123, productName: 'Example Product' }
above typescript code compiled into
function populateProductDetails(product, id, name) {
product.productId = id;
product.productName = name;
}
var newProduct = { productId: 0, productName: '' };
populateProductDetails(newProduct, 123, 'Example Product');
console.log(newProduct); // Output: { productId: 123, productName: 'Example Product' }
o/p:
{productId: 123, productName: 'Example Product'}
10.4
Declare an interface with function type and access its value.
interface MyInterface {
(param1: string, param2: number): boolean;
}
const myFunction: MyInterface = (str, num) => {
return str.length === num;
};
console.log(myFunction('Hello', 5)); // Output: true
console.log(myFunction('World', 10)); // Output: false
ty104.ts
upon compiling, it will generate
.js file
var myFunction = function (str, num) {
return str.length === num;
};
console.log(myFunction('Hello', 5)); // Output: true
console.log(myFunction('World', 10)); // Output: false
o/p:
true
false
Comments
Post a Comment