← Back to TypeScript Course | Chapter 8: Advanced Types | Lesson 8 of 20

satisfies का Operator

satisfies operator जांचता है कि एक expression एक type से conform करे बिना expression के inferred type को बदले। यह उपयोगी है जब आपको precise literal information preserve करते हुए validation चाहिए।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
type Config = { port: number };
const annotated: Config = { port: 8080 };
const satisfied = { port: 8080 } satisfies Config;
console.log(annotated.port, satisfied.port);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. satisfies उपयोग करना और variable के wide type होने की उम्मीद करना, जब यह narrower inferred type रखता है।
  2. वहां satisfies लिखना जहां एक annotation चाहिए, तो variable अब बाद के assignment के लिए interface के रूप में typed नहीं रहता।
  3. satisfies से extra misspelled keys allow होने की उम्मीद करना, जब यह एक object literal पर excess properties report करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.