Typescript - Tips & Tricks - keyof

Full Stack Developer and JavaScript and TypeScript enthusiastic.
Search for a command to run...

Full Stack Developer and JavaScript and TypeScript enthusiastic.
No comments yet. Be the first to comment.
In this series, You'll see some tips and tricks using Typescript
Hi and welcome back! Today I talk about Union and Intersection. In some cases, we have to combine different types to create new types, or sometimes we have parameters that could be of different types. Typescript helps us with two powerful features: U...
Today, I wanna show you how to set up your devices with Antigravity using only the agentic mode, without touching any code. At the last CodeMotion Rome, I got my special badge. A CM Node, a special ha

As a developer, you have to take control of your projects every day. Whether it is a company repository, an open source project you maintain or collaborate on, or a simple pet project. Gaining control over your projects often depends on the platform ...

My Antigravity first experience

Write clear and insightful commit messages using AI and GitLens.

We are in the AI era. New models emerge daily, and many applications have already integrated AI into their workflows. Gemini, OpenAI, Copilot, Deepseek, Llama, and many others enable AI in your applications or help you write code, but they all need a...

Welcome back! Today I'll talk about the keyof operator.
This operator helps us to extract the object's properties such as Literal-types
type Person = {
firstName: string;
surName: string;
age: number;
location: string;
};
type PersonKeys = keyof Person; // "firstName" | "surName" | "age" | "location"
This operator can help us to create new methods that should depend of other types; e.g.
function get<T, K extends keyof T>(obj: T, prop: K): T[K] {
return obj[prop];
}
function set<T, K extends keyof T>(obj: T, prop: K, value: T[K]): void {
obj[prop] = value;
}
but also to create new types from other types e.g
type ReadOnly<T> = {
readonly [K in keyof T]: T[K];
};
type ReadOnlyPerson = ReadOnly<Person>
/*
type ReadOnlyPerson = {
readonly firstName: string;
readonly surName: string;
readonly age: number;
readonly location: string;
}
*/
How we can see this operator is more powerful and it can help us to create new strict type or new strict methods.
That's all for today. See you soon guys!