microsoft / microsoft/TypeScript

support customisable externalHelpersModuleNameText for --importHelpers flag

Open
#12,502 2 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.1.1

Currently we can use --noEmitHelpers with --importHelpers to mitigate generation of bloated code in ever file which uses TS helpers ( extends, decorate ... etc ).
To make this work, currently only official tslib is supported which is hardcoded here

if I wanna override any of these helpers I cannot use --importHelpers, instead I need to import whole tslib and override helpers that I need before any code in app is executed, which is not very convenient, like here:

import 'tslib';

// Babel implementation of _inherits
function _inherits(subClass, superClass) {
  if (typeof superClass !== "function" && superClass !== null) {
    throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  }
  subClass.prototype = Object.create(superClass && superClass.prototype, {
    constructor: {
      value: subClass,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });
  if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}

(window as any).__extends = _inherits;

What I would like to have is flag in compilerOptions for setting node_module library name which should be used for --importHelpers.

Something like:

npm install --save my-tslib

{
  "compilerOptions": {
     "module": "es2105",
     "target": "es5",
     "noEmitHelpers": true,
     "importHelpers": true,
     "importHelpersLibrary": "my-tslib"
  }
}
// Hello.tsx
import * as React from 'react';
class Hello extends React.Component {}

will compile to:

// Hello.jsx
import * as tslib_1 from 'my-tslib';
import * as React from 'react';

var Hello = (function (_super) {
  tslib_1.__extends(Counter, _super);
  function Counter() {
        var _this = _super.apply(this, arguments) || this;
        return _this;
  }
  return Hello;
}(React.Component));

This will also help to support libraries like skate.js and use for instance Babel extends behaviour to make it work https://github.com/skatejs/skatejs/issues/936

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the hardcoded tslib reference in src/compiler/utilities.ts and trace how compilerOptions are parsed and how --importHelpers emits imports. Done means a configurable module name is accepted and emitted for --importHelpers, while the existing default behavior remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.