1

The Generic part in below line in abc which is in <> .

Actually as I am coming from Javascript side to Typescript side in angular 2 .

That's why I am not seen these Generic part in Javascript .

export interface Animals<abc>

export class WaterAnimal <abc> implements Animals<abc> { }

Things which I get :

  1. Generic Part helps to maintain the type which we are sending and achieving from any function will be of same type that type can be any Object,String,Number or Array.

Eg :-

function identity<T>(arg: T): T {
    return arg;
}

Things I want to Ask:-

  1. When We come in Classes and Interfaces :- by passing the parameters in Interface as in Animals interface what is the purpose of it .

  2. If there is any purpose which i don't aware of yet ,now if we implements that Animal Interface to WaterAnimal Class then I want to write <abc> infront of Animal Interface because while defining that Interface I have pass that thing as parameter into it .So Why complier is forcing me to define <abc> with the WaterAnimal Class ..Is there is any Reason behind this..

Any help will be appreciated .

1

1 Answer 1

3

A good example of Generics exists in the language itself.

Most languages had forms on un-typed lists, like the JavaScript Array:

var arr = [
    1,
    "str",
    new Customer()
];

But a much more useful object is an array that only contains a single type - because then you can iterate the array knowing everything is the same:

var arr: NumberArray = [
    1,
    3,
    5
];

But we would need to create a new type for each kind of array, so we would end up with NumberArray, StringArray, CustomerArray... and so on.

So instead of creating a nearly infinite number of types, we create a generic one. We then specify what type we intend to use:

var arr: Array<number>= [
    1,
    3,
    5
];

The type argument (number) allows us to re-use the Array class or interface rather than create new kinds. The condition on this is that Array cannot know anything about this type unless we specify a type constraint.

To understand how this can be used within a generic class, here is a quick fake example:

class ArrayOfOne<T> {
    private item: T;

    add(item: T) {
        this.item = item;
    }

    retrieve() : T {
        return this.item;
    }
}

In this example, the ArrayOfOne class doesn't need to know what T is, but if you use it, T will be strongly typed.

var arr = new ArrayOfOne<string>();
arr.add("str"); // ok
arr.add(1); //not ok

var str: string = arr.retrieve(); // ok
var num: number = arr.retrieve(); // not ok
Sign up to request clarification or add additional context in comments.

1 Comment

Nice Explain . But I dont knw why my editor is forcing me to put <string> when I make export interface Animals<abc> export class WaterAnimal <abc> implements Animals<abc> { } Basically I am asking for second point in Question above...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.