Difference between using Generics and "any" in Typescript?

Asked

Viewed 295 times

7

When I know it’s better to use Generics or any in Typescript?

1 answer

9


The ideal is not to use any. You can live without it, only some cases can be more work, so there are rare cases that can be interesting to use. any is to go 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 using has some link with Javascript that has no type (remembering that it is possible have the types in JS if they are declared separately).

For use in TS itself, you use when you want a facility that routine really accepts anything and makes the same algorithm in it. If you start having too much if to check the guy and decide what to do is already wrong. In some cases it may 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 by laziness or by being more accustomed to JS. These are bad reasons for its use and the code is already doomed, because it will not be the only mistake that this person will make.

Even when an object can have more than one type, it does not need to have all, so one 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’s only when it really accepts all kinds, that it makes no difference what comes, and that’s very rare.

Even before a Union type should prefer another polymorphism solution, for example Generics, so it is specifying a type to be used in the so-called major method or type. You can make a generic/generalized data method or structure, as it says, but it will be treated specifically when calling and this will be safer.

Generics It’s used when something needs to process more than one type, but it wants every call to that type to be different, but it does the same thing. It resembles the Union type because there’s usually some restraint, you can’t wear any kind of fact, but some that make sense, so there’s a Constraint telling what kind of father is accepted there. Unlike the Union type that allows uniting diverse types, the generic only accepts a type that establishes inheritance.

I show example where a unique type is not ideal.

In practice the execution of both will be the same, but in the compilation the any accepts everything, the Generics may have a restriction. If you don’t use a constraint, which is almost always an error, then it turns out to be the same thing in Typescript, because after JS is generated it all turns any even 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;
}

Already this changes a little:

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

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

Because thus the first requires the two parameters to be of the same type, whereas 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 in the Github for future reference.

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

  • Very good, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.