Typescript - Tips & Tricks - Advanced Types

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 guy and welcome back :) Today I'll talk about the Rest Parameters and the Rest Arguments. Sometimes we need to write a simple code like this: function multiply(a: number, b: number): number { return a * b; } const result1 = multiply(2, 2); // 4...
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...

Hi Guys, Today I'll show you some advanced utilities exposed by the typescript language. Let's start!
This utility allows you to add the optional modifier to all the properties of your type.
type PartialPoint = Partial<{ x: number; y: number }>; // { x?: number | undefined; y?: number | undefined; }
This utility allows you to remove the optional modifier to all the properties of your type.
type RequiredPoint = Required<{ x?: number; y?: number }>; // { x: number; y: number; }
This utility allows you to add the read-only modifier to all the properties of your type.
type ReadOnlyPoint = ReadOnly<{ x: number; y: number }>; // { readonly x: number; readonly y: number; }
This utility allows you to get a subset of properties from another type.
type PickPoint = Pick<{ x: number; y: number }, "x">; // { x: number; }
This utility allows you to exclude some properties from another type.
type OmitPoint = Omit<{ x: number; y: number }, "x">; // { y: number; }
This utility allows you to map the properties of a type to another type.
type Point = {
x: number;
y: number;
};
type PointRecord = Record<keyof Point, number | null>; // { x: number | null; y: number | null; }
This utility allows you to remove some types from a union type.
type ExcludeType = Exclude<"x" | "y", "x">; // "y"
This utility allows you to get some types from a union type.
type ExtractType = Extract<"x" | "y", "x">; // "x"
This utility allows you to remove the undefined and null types from a union type.
type NonNullableType = NonNullable<string | number | undefined | null>; // string | number
This utility allows you to extract from a function its parameters.
function createPoint(x: number, y: number) {
return { x, y };
}
type ParametersType = Parameters<typeof createPoint>; // [x: number, y: number]
This utility allows you to extract from a class its constructor parameters.
class Point {
constructor(x: number, y: number) {}
}
type ConstructorParametersType = ConstructorParameters<typeof Point>; // [x: number, y: number]
This utility allows you to get the return type from a function.
function createPoint(x: number, y: number) {
return { x, y };
}
type CreatePointReturnType = ReturnType<typeof createPoint>; // { x: number; y: number; }
This utility allows you to construct a type consisting of the instance type of a construction function type.
class Point {
constructor(x: number, y: number) {}
}
type InstanceTypePoint = InstanceType<typeof Point>; // Point
type InstanceTypeString = InstanceType<string>; // Error: Type 'string' does not satisfy the constraint 'new (...args: any) => any'
This utility allows you to extract the this parameter from a function type, or unknown if the function type has no this parameter.
function toHex(this: Number) {
return this.toString(16);
}
function numberToString(n: ThisParameterType<typeof toHex>) {
return toHex.apply(n);
}
numberToString(12)
This utility allows you to remove the this parameter from a function type.
function toHex(this: Number) {
return this.toString(16);
}
const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5);
fiveToHex()
This utility allows converting your types to uppercase format.
type StringUppercase = Uppercase<"getX" | "getY" | "setX" | "setY">; // "GETX" | "GETY" | "SETX" | "SETY"
This utility allows you to convert your types to lowercase format.
type StringLowercase = Lowercase<"getX" | "getY" | "setX" | "setY">; // "getx" | "gety" | "setx" | "sety"
This utility allows you to convert your types to capitalize format.
type StringCapitalize = Capitalize<"getX" | "getY" | "setX" | "setY">; // "GetX" | "GetY" | "SetX" | "SetY"
This utility allows you to convert your types to uncapitalize format.
type StringUncapitalize = Uncapitalize<"GetX" | "GetY" | "SetX" | "SetY">; // "getX" | "getY" | "setX" | "setY"
I hope these utilities will help you in the future. That's all for today. See you soon Guys!