← Back to TypeScript Course | Chapter 21: Symbols and Iterators | Lesson 2 of 6

Unique Symbols की जानकारी

एक unique symbol एक symbol type है जिसकी type level पर एक specific identity होती है। TypeScript unique symbols का उपयोग करता है जब एक particular symbol constant को हर दूसरे symbol से distinguishable होना चाहिए।
Syntax
typescript
const sym: unique symbol = Symbol("description");

Declaring unique symbols

एक const symbol declaration को special unique symbol type मिलता है जब TypeScript को इसकी exact identity type level पर track करनी हो, न सिर्फ इसे general symbol type मानकर।

उदाहरण: Declaring unique symbols

typescript
const idKey: unique symbol = Symbol("id");
console.log(idKey);

Unique Symbol as a Discriminant

Unique symbols एक union में exact discriminants के रूप में act कर सकते हैं, क्योंकि हर एक precisely एक variant identify करता है — TypeScript यह narrow कर सकता है कि कौन सा specific unique symbol present है, सिर्फ 'कोई symbol' नहीं।

उदाहरण: Unique Symbol as a Discriminant

typescript
const circleTag: unique symbol = Symbol("circle");
const squareTag: unique symbol = Symbol("square");
type Shape =
  | { tag: typeof circleTag; radius: number }
  | { tag: typeof squareTag; side: number };
const s: Shape = { tag: circleTag, radius: 5 };
console.log(s.tag === circleTag);

Unique Symbols as Object Keys

एक unique symbol एक computed property key के रूप में उपयोग किया जा सकता है, एक interface को general symbol type के किसी arbitrary symbol के बजाय exactly एक symbol-keyed property refer करने देते हुए।

उदाहरण: Unique Symbols as Object Keys

typescript
const idKey: unique symbol = Symbol("id");
interface Entity {
  [idKey]: number;
}
const e: Entity = { [idKey]: 1 };
console.log(e[idKey]);

Exact Symbol Identity

दो unique symbols अलग types माने जाते हैं भले ही उनकी description strings identical हों — यह identity-based typing TypeScript को distinct, non-forgeable tokens represent करने का एक कहीं ज़्यादा strong तरीका देती है।

उदाहरण: Exact Symbol Identity

typescript
const a: unique symbol = Symbol("token");
const b: unique symbol = Symbol("token");
console.log(a === (b as unknown as typeof a));

Practical Symbol Tokens

Unique symbols internal tokens के रूप में अच्छे से काम करते हैं जब एक API को strongly-typed identifiers चाहिए जिन्हें कभी गलती से एक ordinary string, number, या किसी और unrelated symbol के साथ confuse न किया जा सके।

उदाहरण: Practical Symbol Tokens

typescript
const AdminToken: unique symbol = Symbol("admin");
function checkAccess(token: typeof AdminToken) {
  return "Access granted";
}
console.log(checkAccess(AdminToken));
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. symbol को let या var से declare करना और एक unique symbol type की उम्मीद करना, जब सिर्फ const declarations qualify करती हैं।
  2. same description वाले दो unique symbols compare करना और types match होने की उम्मीद करना, जब हर एक अपना खुद का type है।
  3. type position में symbol उपयोग करते समय typeof भूल जाना, जैसे tag: idKey बजाय tag: typeof idKey के।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.