Languages
[Edit]
EN

TypeScript - covariance concept

10 points
Created by:
Welsh
902

In this short article, we would like to explain covariance concept in TypeScript.

Main concept:

Covariance concept.
Covariance concept.

Main definition:

GenericType<...> types are covariant if it preserves the ordering of types (), which orders types from more specific to more abstract, so:

if BaseType ≤ SubType, then GenericType<BaseType> ≤ GenericType<SubType>.

In simple worlds:

Covariance alows to cast, without any restrictions, some specific types to more abstract types when composed types are used.

 

Practical examples

Example 1:

class Animal {
    public name: string;
    public constructor(name: string) {
        this.name = name;
    }
}

class Cat extends Animal {
    public mousehunter: boolean;
    public constructor(name: string, mousehunter: boolean) {
        super(name);
        this.mousehunter = mousehunter;
    }
}

const createTom = async (): Promise<Cat> => { return new Cat('Tom', true); };
const createAnimal = async (): Promise<Animal> => { return await createTom(); };
//                             ^^^^^^^^^^^^^^^             ^^^^^^^^^^^^^^^^^
//                                    |                            |
//  According to covariance concept we can cast Promise<Cat> to Promise<Animal>.

// ...

 

Example 2:

class Animal {
    public name: string;
    public constructor(name: string) {
        this.name = name;
    }
}

class Cat extends Animal {
    public mousehunter: boolean;
    public constructor(name: string, mousehunter: boolean) {
        super(name);
        this.mousehunter = mousehunter;
    }
}

const cats: Array<Cat> = [
    new Cat('Tom', true),
    new Cat('Simba', false),
    new Cat('Nala', false)
];

const animals: Array<Animal> = cats;
//             ^^^^^^^^^^^^^   ^^^^
//                   |          |
//  According to covariance concept we can cast Array<Cat> to Array<Animal>.

// ...

 

References

  1. Covariance and contravariance (computer science) - Wikipedia
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join