Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

Why are rest parameters undefined in TypeScript?

$
0
0

I have a React Native app that I'm programming with TypeScript (3.6.3). I have the following code (the actual code is from an API library but this is a minimum reproducable example):

class Base{
    someVal: string[];
    constructor() {
        this.someVal = [];
    }
    someMethod<T extends Base>(this: T, ...someArgs:string[]){
        debugger;
        this.someVal = someArgs;
    }
}

class Derived extends Base{

}

let myVar = new Derived().someMethod('hello');

The code exactly mimics the library code, and behaves the same (erroneous) way. There are no compiler errors or warnings. When I run the code, I expect someArgs to be ['hello'], but it is undefined. Also, I've got an arguments array which contains the actual value of ['hello']:

enter image description here

At this point the code (which is transpiled by Babel on the fly) is acting like Javascript (hence, the undefined actual variable and the phantom arguments variable). Why isn't it transpiled correctly and how do I fix it? (I'm on Babel core/runtime 7.6.2)

Here's the relevant code from the generated index.bundle:

    var Base = function () {
    function Base() {
      (0, _classCallCheck2.default)(this, Base);
      this.someVal = [];
    }

    (0, _createClass2.default)(Base, [{
      key: "someMethod",
      value: function someMethod() {
        debugger;

        for (var _len = arguments.length, someArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
          someArgs[_key - 1] = arguments[_key];
        }

        this.someVal = someArgs;
      }
    }]);
    return Base;
  }();

  var Derived = function (_Base) {
    (0, _inherits2.default)(Derived, _Base);

    function Derived() {
      (0, _classCallCheck2.default)(this, Derived);
      return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Derived).apply(this, arguments));
    }

    return Derived;
  }(Base);

  var myVar = new Derived().someMethod('hello');

Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>