Skip to content

Imports Removed By Compiler When Only Used In JSX Property #12226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
julius-e opened this issue Nov 14, 2016 · 2 comments
Closed

Imports Removed By Compiler When Only Used In JSX Property #12226

julius-e opened this issue Nov 14, 2016 · 2 comments
Labels
Bug A bug in TypeScript Needs More Info The issue still hasn't been fully clarified

Comments

@julius-e
Copy link

TypeScript Version: 2.0.3

I'm migrating a React project from JS to TS. I have the compiler set to allow JS. The TS compiler is transpiling the TS, JS, and JSX to es5.

I've noticed that the TS compiler is dropping imports that are only used in JSX properties. It seems that the compiler doesn't notice the usage of the import and "optimizes" it away.

Also looked at: #4717

Code

import * as React from "react";
import { Comp } from 'components/Comp';
import * as store from '../../js/stores/store';  // <-- Is dropped in the compiled version of this file
class MyView extends React.Component {
  //...
  render() {
    return <div>
      // Other stuff...
      <Comp store={store} />  // Now when render() is run, this throws an exception since store is undefined
    </div>;
  }
}

I've found that this restores the import and fixes things, but it is suboptimal:

import * as React from "react";
import { Comp } from 'components/Comp';
import * as store from '../../js/stores/store';
class MyView extends React.Component {
  //...
  render() {
    let myStore = store;  // Use store in a "value position" in plain JS
    return <div>
      // Other stuff...
      <Comp store={store} />
    </div>;
  }
}
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Nov 14, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.2 milestone Nov 14, 2016
@mhegazy mhegazy modified the milestones: TypeScript 2.1.3, TypeScript 2.2 Nov 15, 2016
@yuit
Copy link
Contributor

yuit commented Nov 15, 2016

@julius-e I can't repo you example. Here is my setup. Let me know if I miss something

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "jsx": "react",
        "allowJs": true,
        "outDir": "OutputJS"
    }
}

main.tsx

import * as React from "react";
import { Comp } from './components/Comp';
import * as store from './inputJS/store';  // <-- Is dropped in the compiled version of this file
class MyView extends React.Component<{},{}> {
  //...
  render() {
    return <div>
      // Other stuff...
      <Comp store={store} />  // Now when render() is run, this throws an exception since store is undefined
    </div>;
  }
}

components/Comp.tsx

export function Comp(prop1: {store: number}): JSX.Element {
    return undefined;
}

inputJS\store.js

"use strict";
export var store = 10;
module.exports = store;

output for main.tsx

"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require("react");
var Comp_1 = require('./components/Comp');
var store = require('./inputJS/store'); // <-- Is dropped in the compiled version of this file
var MyView = (function (_super) {
    __extends(MyView, _super);
    function MyView() {
        _super.apply(this, arguments);
    }
    //...
    MyView.prototype.render = function () {
        return React.createElement("div", null, 
            // Other stuff...
            "// Other stuff...", 
            React.createElement(Comp_1.Comp, {store: store}) // Now when render() is run, this throws an exception since store is undefined
             // Now when render() is run, this throws an exception since store is undefined
            , 
            "  // Now when render() is run, this throws an exception since store is undefined");
    };
    return MyView;
}(React.Component));

Let me know if I miss anything.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Nov 16, 2016
@julius-e
Copy link
Author

I'm having trouble reproducing it on its own as well. The original code still produces the issue, but I cannot share that. This can be closed for now. If I can figure out how to repro, I will reopen.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

4 participants