TypeScript Code


type StringOrNumber = string | number;

type User = {
name: string;
age: number;
}

const userDetails = (
id: StringOrNumber,
user: User
) => {
console.log(User's name is: ${user.name} and his/her age is: ${user.age} and his/her id is: ${id})
}

const sayHello = (user : User) => {
console.log(hello Mr. ${user.name} you are ${user.age} years old.)
}

sayHello({name: 'mahmud', age:25})

let myFunc : (x: number, y: number) => number;

myFunc = (a,b) => {
return a+b;
}

console.log(myFunc(9,10));

class Player{
name: string;
age: StringOrNumber;
country: string;

constructor(n: string, a: number, c: string){
this.name = n;
this.age = a;
this.country = c;
}
play(){
console.log(${this.name} is playing his/her age is ${this.age} from ${this.country});
}
}

const mashrafi = new Player('Mashrafi', 34, 'Bangladesh');

mashrafi.play();

type userInterface = {
name: string;
age: number;
}

const addID = <T extends userInterface>(obj: T) => {
const id = Math.floor(Math.random() * 100);
return { ...obj, id };
}

const obj = {
name: "Mahmud",
age: 25
}

// const str = "Mahmud";

const objectID = addID(obj);

console.log(objectID.name)

// console.log(addID(str));

console.log('----------------------------------')

interface helloAPI {
something: string;
code: number;
}

interface APIResponse<T extends helloAPI, T1>{
status: number;
type: T1;
data: T;
}

const apiResponse1: APIResponse<helloAPI, string> = {
status: 200,
type: 'OK',
data: {
something: 'data',
code: 101,
}
}

console.log(apiResponse1);

class student{
private name: string;
public rool: number;

constructor(name, rool){
this.name = name;
this.rool = rool;
}

displayInfo():string{
return ${this.name}'s rool number is: ${this.rool}
}
}

const st1 = new student('Babu',1001);

console.log(st1);
console.log(st1.displayInfo());