Difference between using generics and" any " in TypeScript?

When I know it is preferable to use generics or any in TypeScript?

Author: Maniero, 2020-08-05

1 answers

The ideal is not to use any. You can live without it, only some cases can give more work, so there are rare cases that can be interesting to use. any is back to being JavaScript and every problem it causes.

Every time you use this type you are admitting that you do not know the type and the most appropriate is to use has some connection with JavaScript that does not have has type (remembering that it is possible to have the types in JS if they are declared separately).

For use in TS even, you use when you want a facility that that routine actually accepts anything and does the same algorithm on it. If you start having too much if to check the type and decide what to do is already wrong. In some cases it can be useful, but it is quite rare and I would leave it to someone very experienced to do it, in doubt do not use any.

I see a lot of programmer use any out of laziness or being more used to JS. These are terrible reasons for its use and the code is already condemned, not least because it will not be the only mistake that person will make.

Even when an object can have more than one type, it does not need to have all, so a Union type can be used. The idea is to be as specific in type as possible, if you don't give a type, try 2 or 3. All types ( any ) it is only when it actually accepts all types, that it makes no difference what comes, and this is very rare.

Even before a union type should prefer another polymorphism solution, for example generics, so so is specifying a type to be used in the method call or larger type. You can do a generic/generalized method or data structure, as he himself says, but it will be treated specifically when calling and that will be safer.

Generics is used when something needs to process more than one type, but wants every call to that type to be different, but does the same thing. He himself resembles the Union type because it usually has some restriction, you can not use any kind of fact, but some that make sense, so there is a constraint saying what kind of father is accepted there. Unlike the union type which allows joining multiple types, the generic only accepts one type that establishes inheritance.

I show example where a single type is not ideal .

In practice the execution of both will be equal, but in compilation any accepts everything, generics may have a constraint. If you do not use a constraint, which is almost always an error, then it turns out to be the same thing in TypeScript, because after generating JS everything turns any because that's all it has in JS (in other languages there are other advantages). Same effect:

function funcao<T>(p: T): T {
    return p;
}

function funcao(p: any): any {
    return p;
}

This already changes a little:

function funcao<T>(p1: T, p2: T): T {
    return p1;
}

function funcao(p1: any, p2 : any): any {
    return p1;
}

Because so the first requires that the two parameters be of the same type, and the second code does not require this, it is already an important semantic difference.

And this changes a lot:

function funcao<T extends number>(p1: T): T {
    return p1 + p2;
}

function funcao(p1: any): any {
    return p1 + p2;
}

I put on GitHub for future reference .

With the constraint you are setting a limit of what you can use, the former may give error if it is not possible to sum the objects.

 8
Author: Maniero, 2020-08-06 11:51:32