Inquirer, Chalk, Chalk-Animation NPM reference links
https://www.npmjs.com/package/inquirer
https://www.npmjs.com/package/chalk
https://www.npmjs.com/package/chalk-a...
calculator using inquirer
import inquirer from 'inquirer'
let answers = await inquirer.prompt([
{
name: "num1",
type: "number",
message: "enter first number"
},
{
name: "num2",
type: "number",
message: "enter second number"
},
{
name: "operation",
type: "list",
choices:["addition", "subtraction", "division", "multiplication"],
},
]);
if(answers.operation==="addition"){
console.log(`The sum of two numbers = ${answers.num1+answers.num2}`)
}
else if(answers.operation==="subtraction"){
console.log(`The differrence of two numbers = ${answers.num1-answers.num2}`)
}
else if(answers.operation==="division"){
console.log(`The division of two numbers = ${answers.num1/answers.num2}`)
}
else if(answers.operation==="multiplication"){
console.log(`The multiplication of two numbers = ${answers.num1*answers.num2}`)
}
Union Types:
function displayValue(value: number | string): void {
console.log(`The value is: ${value}`);
}
displayValue(42); // Output: The value is: 42
displayValue("Hello"); // Output: The value is: Hello
Narrowing:
Narrowing is a concept in TypeScript where you can refine the type of a variable within a certain code block based on runtime checks. This is often done using if statements to check conditions and narrow down the possible types of a variable.
function printLength(value: string | number): void {
if (typeof value === "string") {
console.log(`Length of string: ${value.length}`);
} else if (typeof value === "number") {
console.log(`Value is a number: ${value}`);
} else {
console.log("Value is not a string or number.");
}
}
printLength("Hello"); // Output: Length of string: 5
printLength(42); // Output: Value is a number: 42
printLength(true); // Output: Value is not a string or number.
Type Aliases:
Type Aliases Are Not JavaScript
Type aliases, like type annotations, are not compiled to the output
JavaScript. They exist purely in the TypeScript type system.
Combining Type Aliases
type Id = number | string;
// Equivalent to: number | string | undefined | null
type IdMaybe = Id | undefined | null;
//example 1
type Name = {firstName: string, lastName: string};
let fullName: Name = {firstName: Nouman, lastName: Attique};
// example 2
type personType{
name: {firstName: string, lastName: string},
age: number,
teaching:boolean,
dob: Date,
certification: string[],
}
let person1: personType;
person1 = {
name: {firstName: "nouman", lastName: "Attique"},
age: 30,
teaching: true,
dob: new Date(),
certification: ["AWS", "Docker", "Rust"],
};
//example 3
type Book = {
author: {
firstName: string;
lastName: string;
};
name: string;
}
let book1: Book;
book1 = {
author: {firstName: "Niccolo", lastName: "Machiavelli"},
name: "The Prince";
};
Interface:
interface Person {
firstName: string;
lastName: string;
age: number;
}
// Create an object that adheres to the interface
const person: Person = {
firstName: "John",
lastName: "Doe",
age: 30,};
// Access the properties of the object
console.log(`Name: ${person.firstName} ${person.lastName}`);
console.log(`Age: ${person.age}`);
Type aliases and interfaces in TypeScript are both used to define the structure of objects, but they have some differences:
Type Alias:
Type aliases let you create a new name for a type. You're essentially giving a nickname to a specific structure of data.
They are flexible and can be used with different kinds of types, like unions, intersections, and more.
Type aliases are often used when you want to simplify complex types or create custom combinations of existing types.
You create a type alias using the type keyword.
Interface:
Interfaces are more focused on defining the shape of objects and classes. They describe the contract that an object must adhere to.
They are a standard way of defining object structures, especially when working with classes and their instances.
Interfaces can be extended and implemented by other interfaces and classes.
Interfaces are often used when you want to create a consistent structure for objects that share common properties.
You create an interface using the interface keyword.