Home > OS >  What's the difference between <T extend any> and <T>?
What's the difference between <T extend any> and <T>?

Time:09-27

I've seen some times <T extend any> in a any d.ts files. I don't know what's the difference between <T extend any> and .

Because T extend any never giving restriction to T therefore I think there is no difference to '' that allow anything could pass to the generics.

CodePudding user response:

There's absolutely no difference. <T> and <T extends any> are equivalent.

Let's take an example

class MyClass<T> {
    myValue: T

    constructor(myValue: T) {
        this.myValue = myValue
    }
}

class MyAnotherClass<T extends any> {
    myValue: T

    constructor(myValue: T) {
        this.myValue = myValue
    }
}

The once you compile this both classes generate the same js code.

"use strict";
var MyClass = /** @class */ (function () {
    function MyClass(myValue) {
        this.myValue = myValue;
    }
    return MyClass;
}());
var MyAnotherClass = /** @class */ (function () {
    function MyAnotherClass(myValue) {
        this.myValue = myValue;
    }
    return MyAnotherClass;
}());
//# sourceMappingURL=sample.js.map
  • Related