Awaited<Type>
ใช้สำหรับรับค่าที่ถูก resolve จาก Promise
type A = Awaited<Promise<string>>; // string
type B = Awaited<Promise<Promise<number>>>; // number
Partial<Type>
ทำให้ทุก property ใน Type กลายเป็นตัวเลือก (optional)
interface User {
name: string;
age: number;
}
type PartialUser = Partial<User>;
// ทุก property เป็น optional
// { name?: string; age?: number; }
Required<Type>
ทำให้ทุก property ใน Type เป็นค่าที่จำเป็นต้องมี (required)
interface Config {
host?: string;
port?: number;
}
type RequiredConfig = Required<Config>;
// ทุก property เป็น required
// { host: string; port: number; }
Readonly<Type>
ทำให้ทุก property ใน Type เป็นแบบอ่านอย่างเดียว (readonly)
interface Point {
x: number;
y: number;
}
type ReadonlyPoint = Readonly<Point>;
// { readonly x: number; readonly y: number; }
Record<Keys, Type>
สร้าง object type ที่มี property เป็น Keys และมี type เป็น Type
type Fruit = "apple" | "banana" | "orange";
type FruitCounts = Record<Fruit, number>;
// { apple: number; banana: number; orange: number; }
Pick<Type, Keys>
สร้าง type ใหม่โดยเลือกเฉพาะ property ที่ต้องการจาก Type
interface Person {
name: string;
age: number;
address: string;
}
type NameAndAge = Pick<Person, "name" | "age">;
// { name: string; age: number; }
Omit<Type, Keys>
สร้าง type ใหม่โดยละเว้น property ที่ระบุจาก Type
interface Person {
name: string;
age: number;
address: string;
}
type PersonWithoutAddress = Omit<Person, "address">;
// { name: string; age: number; }
Exclude<UnionType, ExcludedUnion>
สร้าง type ใหม่โดยไม่รวม type ที่ระบุใน ExcludedUnion
type T0 = Exclude<"a" | "b" | "c", "a">;
// "b" | "c"
Extract<Type, Union>
สร้าง type ใหม่โดยเลือกเฉพาะ type ที่สามารถ assign ให้กับ Union ได้
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
// "a"
NonNullable<Type>
สร้าง type ใหม่โดยไม่รวม null และ undefined
type T0 = NonNullable<string | number | undefined>;
// string | number
Parameters<Type>
สร้าง tuple type จาก parameter types ของฟังก์ชัน
function greet(name: string, age: number): void {}
type GreetParams = Parameters<typeof greet>;
// [string, number]
ConstructorParameters<Type>
สร้าง tuple type จาก parameter types ของ constructor function
class Person {
constructor(name: string, age: number) {}
}
type PersonConstructorParams = ConstructorParameters<typeof Person>;
// [string, number]
ReturnType<Type>
สร้าง type จาก return type ของฟังก์ชัน
function getName(): string {
return "John";
}
type GetNameReturnType = ReturnType<typeof getName>;
// string
InstanceType<Type>
สร้าง type จาก instance type ของ constructor function
class C {
x = 0;
y = 0;
}
type T0 = InstanceType<typeof C>;
// C
NoInfer<Type>
ป้องกันการ infer type ในบางสถานการณ์
function fn<T>(arg: T, arg2: NoInfer<T>) {
// ...
}
fn(10, 20); // OK
fn(10, "20"); // Error
ThisParameterType<Type>
สร้าง type จาก this parameter ของฟังก์ชัน
function toHex(this: Number) {
return this.toString(16);
}
type T = ThisParameterType<typeof toHex>;
// Number
OmitThisParameter<Type>
สร้าง type ใหม่โดยลบ this parameter ออกจากฟังก์ชัน
function toHex(this: Number) {
return this.toString(16);
}
type T = OmitThisParameter<typeof toHex>;
// () => string
ThisType<Type>
ใช้เพื่อกำหนด this type ในอ็อบเจ็กต์ลิเทอรัล
interface ThisType<T> {}
type ObjectDescriptor<D, M> = {
data?: D;
methods?: M & ThisType<D & M>;
};
Intrinsic String Manipulation Types
Lowercase<StringType>
แปลงตัวอักษรทั้งหมดเป็นตัวพิมพ์เล็ก
type T0 = Lowercase<"HELLO">;
// "hello"
Capitalize<StringType>
แปลงตัวอักษรตัวแรกเป็นตัวพิมพ์ใหญ่
type T0 = Capitalize<"hello">;
// "Hello"
Uncapitalize<StringType>
แปลงตัวอักษรตัวแรกเป็นตัวพิมพ์เล็ก
type T0 = Uncapitalize<"Hello">;
// "hello"