0

I have this:

export class XMLParser extends Transform {

  constructor(opts?: XMLParserOpts, to?: TransformOptions) {

    super(Object.assign({}, to || {}, {objectMode: true}));

  }

}

basically I always need objectMode to be true. However, what if the user passed in some weird value for to? How can I check it to make sure the type is defined an object?

I can let the Transform class do its validation, but I'd rather do that manually and give a custom error message. But the problem is, I can't do this:

   constructor(opts?: XMLParserOpts, to?: TransformOptions) {

        if(to && typeof to !== 'object'){
          throw new Error('no good bozo');
        }

        super(Object.assign({}, to || {}, {objectMode: true}));

    }

how can do what I am looking for? (TS won't compile if super is not the first call in the constructor).

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

0

I guess the best thing to do is something like:

  constructor(opts?: XMLParserOpts, to?: TransformOptions) {

        super((function(){

          if(to && typeof to !== 'object'){
              throw new Error('no good bozo');
           }

           return Object.assign({}, to || {}, {objectMode: true})

        })());

    }

use cases for IIFEs really do pop up don't they.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817