More Typescript APIs
More Typescript APIs
com/pdf/ts-hard/ts-hard-1
Step 1 - Pre-requisites
Before you go through this module, make sure you’ve gone through basic
typescript classes.
You understand interfaces , types and how typescript is used in a simple
Node.js application
interface User {
name: string;
age: number;
}
// Example usage
const result = sumOfAge({
name: "harkirat",
age: 20
}, {
name: "raman",
age: 21
});
console.log(result); // Output: 9
1 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
1. Initialize TS
{
"rootDir": "./src",
"outDir": "./dist"
}
Pick
Pick allows you to create a new type by selecting a set of properties
( Keys ) from an existing type ( Type ).
Imagine you have a User model with several properties, but for a user
profile display, you only need a subset of these properties.
interface User {
id: number;
name: string;
email: string;
createdAt: Date;
}
2 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
Partial
Partial makes all properties of a type optional, creating a type with the
same properties, but each marked as optional.
3 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
interface User {
id: string;
name: string;
age: string;
email: string;
password: string;
};
Readonly
When you have a configuration object that should not be altered after
initialization, making it Readonly ensures its properties cannot be
changed.
interface Config {
readonly endpoint: string;
readonly apiKey: string;
}
4 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
Record
Record let’s you give a cleaner type to objects
interface User {
5 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
id: string;
name: string;
}
or use Record
interface User {
id: string;
name: string;
}
Map
maps gives you an even fancier way to deal with objects. Very similar to
Maps in C++
interface User {
id: string;
name: string;
}
6 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
7 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
Exclude
In a function that can accept several types of inputs but you want to
exclude specific types from being passed to it.
handleEvent('click'); // OK
8 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
9 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
if (!success) {
res.status(411).json({});
return
}
// update database here
res.json({
message: "User updated"
})
});
app.listen(3000);
10 of 11 10/10/24, 15:47
Projects | 100xDevs https://projects.100xdevs.com/pdf/ts-hard/ts-hard-1
11 of 11 10/10/24, 15:47