Skip to content

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

Closed
@julius-e

Description

@julius-e

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>;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptNeeds More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions