Variance हे वर्णन करते की composite type ची subtyping कशी त्याच्या भागांच्या subtyping शी संबंधित आहे — म्हणजे Container<Sub> हे Container<Super> साठी assignable कधी आहे?
चला Dog हा Animal चा subtype असू द्या.
let dogs: Dog[] = [];
let animals: Animal[] = dogs; // ✅ Dog[] is assignable to Animal[]
Return types आणि arrays covariant आहेत: जर Dog ⊆ Animal, तर Dog[] ⊆ Animal[]. Dog परत करणारा function असा काम करतो जेथे Animal परत करणारा function अपेक्षित असतो.
type Handler<T> = (arg: T) => void;
let animalHandler: Handler<Animal> = (a) => {};
let dogHandler: Handler<Dog> = animalHandler; // ✅ (with strictFunctionTypes)
// a handler that accepts ANY Animal can safely handle a Dog
Function parameters contravariant आहेत: Handler<Animal> हा Handler<Dog> साठी assignable आहे, element relationship च्या opposite मध्ये. हे sound आहे — असे काहीतरी जे सर्व animals हँडल करते ते निश्चितपणे dogs हँडल करते.
// Method parameters in TS are bivariant by default (a known unsound convenience)
interface Comparer<T> { compare(a: T): void; }
TypeScript standalone function types ला strictFunctionTypes अंतर्गत केवळ contravariantly check करते; method parameters हे जाणूनबुजून bivariant आहेत ergonomics साठी, जे तांत्रिकदृष्ट्या unsound आहे.
Variance हे स्पष्ट करते कोण काही assignments allow किंवा reject केल्या जातात — का Dog[] Animal[] मध्ये फिट होते पण (d: Dog) => void callback सर्वदा (a: Animal) => void साठी stand in करू शकत नाही.
ही समज तुम्हाला generic APIs डिজाइन करायला मदत करते (उदा. read-only बनाम write positions) आणि functions आणि generics समाविष्ट करणारे confusing "not assignable" errors decode करते.
सविस्तर उत्तरांसह IT मुलाखत प्रश्नांचे ग्रंथालय — Junior पासून Senior पर्यंत.
देणगी द्या