satisfies का Operator
In this page:
const variable = {
property: value,
} satisfies TypeName;
Basic satisfies Usage
satisfies operator यह जांचता है कि एक expression एक required type से conform करे बिना उस type को बदले जो TypeScript असल में उस expression के लिए infer करता है — आपको validation और precise inference एक साथ मिलते हैं।
उदाहरण: Basic satisfies Usage
type Config = { port: number; host: string };
const config = { port: 8080, host: "localhost" } satisfies Config;
console.log(config.port);
Preserving Literal Information
एक broad type annotation के विपरीत, satisfies original expression से specific literal information preserve करता है, तो satisfies से checked एक value annotation के type तक widen होने के बजाय अपना narrow, literal type रखती है।
उदाहरण: Preserving Literal Information
type Colors = Record<string, string>;
const palette = { red: "#ff0000", green: "#00ff00" } satisfies Colors;
console.log(palette.red.toUpperCase());
Checking Object Keys
satisfies record objects के लिए खासतौर पर उपयोगी है जहां आप चाहते हैं कि TypeScript एक required shape के खिलाफ missing या misspelled keys पकड़े, बाद में हर key का specific value type access करने की ability खोए बिना।
उदाहरण: Checking Object Keys
type Routes = Record<"home" | "about", string>;
const routes = { home: "/", about: "/about" } satisfies Routes;
console.log(routes.about);
satisfies with Functions
एक function-valued property satisfies से checked हो सकती है जबकि expression खुद directly callable और exactly जैसा लिखा गया वैसा usable रहती है, इसे एक explicit type annotation से cast करने से अलग।
उदाहरण: satisfies with Functions
type Handlers = Record<string, (x: number) => number>;
const handlers = {
double: (x: number) => x * 2,
} satisfies Handlers;
console.log(handlers.double(5));
satisfies vs Type Annotation
एक type annotation TypeScript को बताता है कि आगे एक value को declared type के रूप में treat करे, जबकि satisfies सिर्फ एक बार compatibility check करता है और फिर expression को अपना ज़्यादा specific inferred type रखने देता है।
उदाहरण: satisfies vs Type Annotation
type Config = { port: number };
const annotated: Config = { port: 8080 };
const satisfied = { port: 8080 } satisfies Config;
console.log(annotated.port, satisfied.port);
satisfiesउपयोग करना और variable के wide type होने की उम्मीद करना, जब यह narrower inferred type रखता है।- वहां
satisfiesलिखना जहां एक annotation चाहिए, तो variable अब बाद के assignment के लिए interface के रूप में typed नहीं रहता। satisfiesसे extra misspelled keys allow होने की उम्मीद करना, जब यह एक object literal पर excess properties report करता है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- TypeScript Advanced Types
- Mapped Types
- Conditional Types
- Custom Type Guards
- Assertion Functions
- Control Flow Analysis
- Exhaustiveness Checking
- Satisfies Operator
- Template Literal Types
- Utility Types - Partial
- Utility Types - Required
- Utility Types - Readonly
- Utility Types - Pick
- Utility Types - Omit
- Utility Types - Record
- Utility Types - Exclude and Extract
- Utility Types - NonNullable
- Utility Types - ReturnType
- Utility Types - Parameters
- TypeScript 5 Updates