Utility Types (ইউটিলিটি টাইপ)
Partial, Required, Pick, Omit, Record, Exclude, Extract, NonNullable, ReturnType, Parameters, ConstructorParameters, InstanceType, Awaited এবং String Manipulation Utility Types।
TypeScript-এর সাথে আসে বিল্ট-ইন Utility Types — যা একটি existing type থেকে নতুন type তৈরি, পরিবর্তন বা রূপান্তর করতে সাহায্য করে। এগুলো ব্যবহার করলে বারবার একই type লিখতে হয় না এবং type-safe code লেখা সহজ হয়।
Partial
Partial<T> একটি type-এর সব property-কে optional করে দেয়।
type User = {
name: string;
age: number;
email: string;
};
const draft: Partial<User> = {
name: "রহিম",
};এখানে name, age এবং email — তিনটিই optional হয়ে গেছে।
Required
Required<T> একটি type-এর সব optional property-কে required করে দেয়।
type User = {
name?: string;
age?: number;
email?: string;
};
type FullUser = Required<User>;
const user: FullUser = {
name: "রহিম",
age: 25,
email: "[rahim@example.com](mailto:rahim@example.com)",
};Readonly
Readonly<T> একটি type-এর সব property-কে readonly করে দেয়।
type Point = {
x: number;
y: number;
};
type FixedPoint = Readonly<Point>;
const point: FixedPoint = {
x: 10,
y: 20,
};
// Error
// point.x = 50;Pick
Pick<T, K> একটি type থেকে নির্দিষ্ট property বেছে নিয়ে নতুন type তৈরি করে।
type User = {
id: number;
name: string;
email: string;
role: string;
};
type UserPreview = Pick<User, "id" | "name">;
const preview: UserPreview = {
id: 1,
name: "রহিম",
};Omit
Omit<T, K> একটি type থেকে নির্দিষ্ট property বাদ দিয়ে নতুন type তৈরি করে।
type User = {
id: number;
name: string;
email: string;
password: string;
};
type PublicUser = Omit<User, "password">;
const user: PublicUser = {
id: 1,
name: "রহিম",
email: "[rahim@example.com](mailto:rahim@example.com)",
};Record
Record<K, T> নির্দিষ্ট key-এর জন্য একটি object type তৈরি করে।
type AgeMap = Record<string, number>;
const ages: AgeMap = {
রহিম: 28,
করিম: 30,
};নির্দিষ্ট key-ও ব্যবহার করা যায়।
type Page = "home" | "about" | "contact";
type PageConfig = Record<Page, { title: string }>;
const pages: PageConfig = {
home: { title: "Home" },
about: { title: "About" },
contact: { title: "Contact" },
};Exclude
Exclude<T, U> একটি union type থেকে নির্দিষ্ট type বাদ দেয়।
type Status = "active" | "pending" | "banned";
type AvailableStatus = Exclude<Status, "banned">;
// "active" | "pending"Extract
Extract<T, U> একটি union type থেকে শুধু matching type-গুলো রাখে।
type T1 = "a" | "b" | "c";
type T2 = "b" | "c" | "d";
type Common = Extract<T1, T2>;
// "b" | "c"NonNullable
NonNullable<T> একটি type থেকে null এবং undefined বাদ দেয়।
type MaybeName = string | null | undefined;
type Name = NonNullable<MaybeName>;
// string
const name: Name = "রহিম";ReturnType
ReturnType<T> একটি function-এর return type বের করে।
function createUser(name: string, age: number) {
return {
name,
age,
createdAt: new Date(),
};
}
type UserShape = ReturnType<typeof createUser>;
// {
// name: string;
// age: number;
// createdAt: Date;
// }Parameters
Parameters<T> একটি function-এর parameter-গুলোকে tuple type হিসেবে বের করে।
function createUser(name: string, age: number) {
return {
name,
age,
};
}
type UserArgs = Parameters<typeof createUser>;
// [name: string, age: number]ConstructorParameters
ConstructorParameters<T> একটি class constructor-এর parameter-গুলোকে tuple হিসেবে বের করে।
class User {
constructor(
public name: string,
public age: number
) {}
}
type UserArgs = ConstructorParameters<typeof User>;
// [name: string, age: number]InstanceType
InstanceType<T> একটি constructor থেকে তৈরি হওয়া instance-এর type বের করে।
class User {
constructor(
public name: string,
public age: number
) {}
}
type UserInstance = InstanceType<typeof User>;
const user: UserInstance = new User("রহিম", 25);Awaited
Awaited<T> একটি Promise-এর ভিতরের resolved type বের করে।
async function fetchUser(id: number) {
return {
id,
name: "রহিম",
};
}
type UserResult = Awaited<ReturnType<typeof fetchUser>>;
// {
// id: number;
// name: string;
// }Nested Promise-এর ক্ষেত্রেও Awaited কাজ করে।
type Result = Awaited<Promise<Promise<string>>>;
// stringThisParameterType
ThisParameterType<T> একটি function-এর explicit this parameter-এর type বের করে।
function greet(this: { name: string }, message: string) {
return `${message}, ${this.name}`;
}
type Context = ThisParameterType<typeof greet>;
// { name: string }OmitThisParameter
OmitThisParameter<T> একটি function type থেকে this parameter বাদ দেয়।
function greet(this: { name: string }, message: string) {
return `${message}, ${this.name}`;
}
type GreetFunction = OmitThisParameter<typeof greet>;
// (message: string) => stringThisType
ThisType<T> object-এর method-এর ভিতরে this কী type হবে তা নির্ধারণ করতে সাহায্য করে।
type State = {
count: number;
};
type Methods = {
increment(): void;
decrement(): void;
};
const store: State & Methods & ThisType<State & Methods> = {
count: 0,
increment() {
this.count++;
},
decrement() {
this.count--;
},
};ThisType<T> সাধারণ application code-এর তুলনায় framework, library এবং advanced object patterns-এ বেশি দেখা যায়।
Uppercase
Uppercase<S> একটি string literal type-এর সব character uppercase করে।
type Greeting = Uppercase<"hello world">;
// "HELLO WORLD"Lowercase
Lowercase<S> একটি string literal type-এর সব character lowercase করে।
type Greeting = Lowercase<"HELLO WORLD">;
// "hello world"Capitalize
Capitalize<S> string-এর প্রথম character uppercase করে।
type Title = Capitalize<"hello world">;
// "Hello world"Uncapitalize
Uncapitalize<S> string-এর প্রথম character lowercase করে।
type Title = Uncapitalize<"Hello World">;
// "hello World"Utility Types এক নজরে
| Utility | কাজ |
|---|---|
Partial<T> | সব property optional |
Required<T> | সব property required |
Readonly<T> | সব property readonly |
Pick<T, K> | নির্দিষ্ট property বাছাই |
Omit<T, K> | নির্দিষ্ট property বাদ |
Record<K, T> | key-value object type তৈরি |
Exclude<T, U> | union থেকে type বাদ |
Extract<T, U> | matching type রাখা |
NonNullable<T> | null ও undefined বাদ |
ReturnType<T> | function-এর return type |
Parameters<T> | function parameter tuple |
ConstructorParameters<T> | constructor parameter tuple |
InstanceType<T> | constructor-এর instance type |
ThisParameterType<T> | this parameter-এর type |
OmitThisParameter<T> | this parameter বাদ |
ThisType<T> | contextual this type |
Awaited<T> | Promise-এর resolved type |
Uppercase<S> | সব character uppercase |
Lowercase<S> | সব character lowercase |
Capitalize<S> | প্রথম character uppercase |
Uncapitalize<S> | প্রথম character lowercase |
Practical Example
Real-world application-এ একাধিক Utility Type একসাথে ব্যবহার করা যায়।
type User = {
id: number;
name: string;
email: string;
password: string;
role: "admin" | "user";
};
// Create form-এর জন্য password optional
// এবং id বাদ
type CreateUserInput = Omit<User, "id">;
// API response থেকে password বাদ
type UserResponse = Omit<User, "password">;
// Update-এর সময় সব field optional
type UpdateUserInput = Partial<UserResponse>;
// শুধু public information
type UserPreview = Pick<User, "id" | "name" | "role">;Utility Types compile-time-এ কাজ করে। এগুলো runtime-এ নতুন কোনো JavaScript code যোগ করে না; বরং existing type থেকে নতুন type তৈরি বা পরিবর্তন করতে সাহায্য করে।
দৈনন্দিন TypeScript এবং Next.js development-এ প্রথমে Partial, Required, Pick, Omit, Record, Exclude, Extract, ReturnType, Parameters এবং Awaited ভালোভাবে আয়ত্ত করুন।