Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Created August 30, 2020 10:41
Show Gist options
  • Save kettanaito/60d3cf9bd9303dd05be2d1f2c45f425c to your computer and use it in GitHub Desktop.
Save kettanaito/60d3cf9bd9303dd05be2d1f2c45f425c to your computer and use it in GitHub Desktop.
TS issue

I have a class called SocketPolyfill that looks roughly like this:

import { EventEmitter } from 'events'

export class SocketPolyfill extends EventEmitter {
	// methods here
}

I build the project with this class using the following tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "strict": true,
    "target": "ES5",
    "module": "CommonJS",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "typeRoots": ["node_modules/@types"],
    "outDir": "lib",
    "declaration": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "/**/*.test.*"]
}

In the built modules, if we look at SocketPolyfill.js, it looks like this:

// SocketPolyfill.js
var events_1 = require("events");

exports.SocketPolyfill = void 0;
var SocketPolyfill = /** @class */ (function (_super) {
  __extends(SocketPolyfill, _super);
	// methods here
}(events_1.EventEmitter))

// At this point `SocketPolyfill` is defined and equals a constructor function, which is OK.

exports.SocketPolyfill = SocketPolyfill;

Then this class is being imported in another built module:

// ClientRequest.js
var SocketPolyfill_1 = require("./polyfills/SocketPolyfill");

// If I log out `SocketPolyfill_1` here it outputs:
// [Object: null prototype] {}
// and has no named export called `SocketPolyfill`.
var socket = new SocketPolyfill_1.SocketPolyfill(options)

When this module is evaluated, I get an exception:

TypeError: SocketPolyfill$1.SocketPolyfill is not a constructor

It looks like although the SocketPolyfill is properly exported, when it’s being imported as a part of the same package it yields {} as the export. Constructing undefined fails.

What can be the issue here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment