The satisfies operator, introduced in TypeScript 4.9, validates that an expression is assignable to a type without widening or changing its inferred type. You get both type-safety and the narrowest possible type.
Toán tử satisfies, được giới thiệu trong TypeScript 4.9, xác thực rằng một biểu thức có thể gán cho một kiểu mà không làm thay đổi kiểu được suy luận của biểu thức đó. Nói cách khác, bạn nhận được cả sự an toàn kiểu lẫn kiểu hẹp nhất có thể.
1// Type annotation widens — 'port' is inferred as string | number2const config: Record<string, string | number> = {3 host: "localhost",4 port: 3000,5};6config.port.toFixed(); // Error: toFixed does not exist on string | number78// satisfies validates without widening — 'port' stays number, 'host' stays string9const config2 = {10 host: "localhost",11 port: 3000,12} satisfies Record<string, string | number>;1314config2.port.toFixed(); // OK — TypeScript knows port is number15config2.host.toUpperCase(); // OK — TypeScript knows host is stringThere are three ways to apply type information to an expression, each with different tradeoffs. Understanding the distinction helps you pick the right tool.
Có ba cách để áp dụng thông tin kiểu cho một biểu thức, mỗi cách có sự đánh đổi khác nhau. Hiểu sự khác biệt sẽ giúp bạn lựa chọn đúng công cụ.
const x: T = ...): widens the inferred type to T. TypeScript forgets the specific literal types. Good when you truly want type T.Chú thích kiểu (const x: T = ...): mở rộng kiểu được suy luận sang T. TypeScript quên đi các kiểu literal cụ thể. Tốt khi bạn thực sự muốn kiểu T.
satisfies T: validates assignability to T but preserves the narrowly inferred type. Best when you need validation without losing type information.satisfies T: xác thực rằng biểu thức có thể gán cho T nhưng giữ nguyên kiểu hẹp được suy luận. Tốt nhất khi bạn cần xác thực mà không mất thông tin kiểu.
as T (type assertion): overrides the inferred type without any checking. Unsafe — TypeScript trusts you unconditionally. Use only as a last resort.as T (ép kiểu): ghi đè kiểu được suy luận mà không kiểm tra gì. Không an toàn — TypeScript tin bạn vô điều kiện. Chỉ dùng khi thực sự cần thiết.
1type Color = "red" | "green" | "blue";23// Annotation: palette is Record<Color, string>, values become string (widened)4const paletteAnnotated: Record<Color, string> = {5 red: "#ff0000",6 green: "#00ff00",7 blue: "#0000ff",8};9// paletteAnnotated.red is 'string', not '#ff0000'1011// satisfies: values keep their literal types12const palette = {Use Case: Key Validation with Preserved Value Types
Trường Hợp Sử Dụng: Xác Thực Khóa với Kiểu Giá Trị Được Giữ Nguyên
The most common pattern for satisfies is applying it to object literals when you want TypeScript to check key completeness and correctness while retaining per-value literal types.
Mô hình phổ biến nhất cho satisfies là áp dụng nó vào các object literal khi bạn muốn TypeScript kiểm tra tính hoàn chỉnh và chính xác của các khóa trong khi vẫn giữ lại kiểu literal cho từng giá trị.
1type Route = "home" | "about" | "contact";23const routeMeta = {4 home: { title: "Home", icon: "house" },5 about: { title: "About", icon: "info" },6 contact: { title: "Contact", icon: "mail" },7} satisfies Record<Route, { title: string; icon: string }>;89// Each value keeps its literal shape:10routeMeta.home.icon; // type is "house", not just string1112// Missing a key is a compile error:satisfies still performs excess property checking on object literals. If a key is not present in the constraining type, TypeScript reports an error at the declaration site — exactly where you want it.satisfies vẫn áp dụng kiểm tra thuộc tính dư thừa (excess property checking) trên object literal. Nếu object có khóa không xuất hiện trong kiểu ràng buộc, TypeScript sẽ báo lỗi ngay tại chỗ khai báo.
Use Case: Config Objects with Mixed Value Types
Trường Hợp Sử Dụng: Cấu Hình với Kiểu Giá Trị Hỗn Hợp
satisfies shines with config objects whose values have different types. A type annotation loses the specific type info; satisfies preserves it for safe downstream access.
satisfies đặc biệt hữu ích cho các object cấu hình có giá trị với kiểu khác nhau. Chú thích kiểu sẽ mất thông tin về kiểu cụ thể, trong khi satisfies giữ nguyên chúng để truy cập an toàn sau này.
1type AppConfig = {2 host: string;3 port: number;4 debug: boolean;5 tags: string[];6};78const config = {9 host: "localhost",10 port: 3000,11 debug: true,12 tags: ["web", "api"],satisfies validates at declaration only. It does not add readonly to the inferred type, so properties remain mutable unless you also use as const or explicit readonly.
satisfies chỉ xác thực tại thời điểm khai báo. Nó không thêm readonly vào kiểu được suy luận, vì vậy nếu biến không phải là const, các thuộc tính vẫn có thể bị gán lại với các kiểu rộng hơn.
satisfies with as const for both type validation and immutability: const x = { ... } as const satisfies T. Order matters — as const is applied first, then satisfies validates that immutable type.Kết hợp satisfies với as const để có cả sự xác thực kiểu lẫn tính bất biến: const x = { ... } as const satisfies T. Thứ tự quan trọng — as const được áp dụng trước, sau đó satisfies xác thực kiểu bất biến đó.
satisfies works on expressions, not function declarations. You cannot write function f() satisfies T. For return type validation, use a standard return type annotation.satisfies không hoạt động trên kiểu trả về của hàm — chỉ dùng được trên biểu thức, không phải trên khai báo hàm. Để xác thực kiểu trả về, dùng chú thích kiểu trả về thông thường.
satisfies validates assignability to a type without widening the inferred typesatisfies xác thực rằng một biểu thức có thể gán cho kiểu mà không làm rộng kiểu được suy luận
T; satisfies keeps the narrow type; as skips all checkingChú thích kiểu mở rộng sang T; satisfies giữ kiểu hẹp; as bỏ qua mọi kiểm tra
satisfies on object literals when you want key validation and preserved literal value typesDùng satisfies trên object literal khi bạn muốn xác thực khóa và kiểu giá trị literal được giữ nguyên
Kiểm tra thuộc tính dư thừa vẫn được áp dụng — các khóa thừa gây ra lỗi biên dịch
as const satisfies T for both immutability and type validationKết hợp as const satisfies T để có cả tính bất biến lẫn xác thực kiểu
satisfies works on expressions only, not function declarationssatisfies chỉ hoạt động trên biểu thức, không phải trên khai báo hàm