12.2 Advance typescript

 create a folder initialize it : npx init -y 
now initialize it with typescript : npx tsc --init

update ts config of "outdir" and "rootdir" to "./dits" and "./src" ,

code
interface user { 
name : string;
age: number;
};

function someOfAge (User1: user , User2: user) {
return User1.age + User2.age;
}
const age = someOfAge({name: " dhrup sinha" , age: 20},  {name: " simran sinha" , age0: 205})
console.log(age);

run the code command : 
tsc -b 
OR
npx tsc -b 

Pike: 
The Pike general type in Script is a powerful feature that allow you to constructs new type by selecting a subset of properties from on existing type / Interface, enhance type safety and code readability without redundancy.

syntax:- Pike < type key | 'key' | 'key' >;

code
Interface user(
id :string;
name: string;
age: number;
email: string;
password: string;
};

type UpdateProp = Pike< User, "name" | 'age' | 'email'>;

function updateUser ( updateProps: updateprops) {
//rest code
}

Partial 
Partial make all property of a type optional,  creating a type wity the same properties, but each marter is optional.

code
  From πŸ‘‡ this

Interface user(
id :string;
name: string;
age: number;
email: string;
password: string;
};

TO πŸ‘‡This

Interface user(
id ?:string;
name?: string;
age?: number;
email: string;
password: string;
};


 Read only:
As per the name it is not altered after initialization, making it readonly ensure its properties cannot be changed.

code 
type user = { 
readonly name : string;
readonly age: number;
}
const user: user: User = { 
name: john ;
age : 21;
}
user.age=12;

        πŸ…žπŸ…‘

type user = { 
readonly name : string;
readonly age: number;
}
const user: readonly < user> = { 
name : ' john' ;
 age: 23;
}
user.age=12;

Record :
Record lets you give a cleaner type of objects yo can type objects like followers

without record 
type userAge = { [key: string]: number; //here number is represent key's value also it represent type of value 

with record
type users = Record<string, number> ;
const users: usersAge= {
" Ramm "| : 21,
"kaka" : 69
}

Maps

It is another way to do key value pair and map will doit
map gives you an even fancier way to deal with objects very similar to maps in c++

code
const users = new map ()
users.set ( "ras@qd", {name: "raj", age: 30 , email: "ras@qd@gmail.com"})
users.set ( "sarah@qd1", {name: "sanah", age: 32 , email: "sarah@qd1@gmail.com"})
cosnt user = users.get("user");
console.log(user);

code
type User = {
name: string;
age: number;
email:string;
}
const user = new map< string, User> ()

Exclude
In a function that can accept several types of input but you want to exclude speciticiel type from bein passed to it.

code

type EventType = ' click' | 'scroll' | 'mouse move' ;
type ExcludeEvenet = exclude < eventType, 'scroll' > ;
const handleEvent = (event : ExcludeEvent) =>  {
console.log(`Handling event: ${event}`);
};
handleEvent('click') ; //ok

zod Interface

when we are using zod, we are done you can extract the typeScript of any schema with  z.infer< type of my schema> ;
const A = z.string();
type A = z.infer<type of A> ; //string
const U :  A12;  //type error
const U : A = 'asd'; //coniles

Comments

Popular posts from this blog

CyberSecurity

VERTICAL SCALING πŸ’‹

prisma using in project idx 13.3