-
Notifications
You must be signed in to change notification settings - Fork 887
JSX does not mark React
as used when imported via import React = require("react");
#893
Comments
Good catch @cezary-biernacki, thanks! Is using |
@cezary-biernacki @jkillian - according to the spec the two are equivalent. So whilst the rule should be fixed for completeness, @cezary-biernacki you should be safe to use the |
@jkillian: Yes, the workaround is acceptable, just it is confusing. I had to debug TSLint itself to discover what kind of syntax works, as the documentation does not provide necessary details. |
Another variant of this: TSLint does not mark EDIT: I ended up using this: https://www.npmjs.com/package/babel-plugin-react-require which automatically injects an import of React for stateless components. |
well it's a bit more than that; Babel automatically creates synthetic default exports. but I agree that the |
There is another issue related to the original problem #689 and it affects imports in jest unit-test cases. The linter also doesn't mark react as used when calling import * as React from 'react'; // creates error
import * as ReactDOM from 'react-dom';
import * as TestUtils from 'react-addons-test-utils';
describe('Input', () => {
it('just an input test', () => {
// this statement should be detected as usage of the react import
const input = TestUtils.renderIntoDocument(<input type = 'text'/>);
const inputNode: Element = ReactDOM.findDOMNode(input);
inputNode.setAttribute('value', 'test');
expect(input.getAttribute('value')).toEqual('test');
});
}); |
We are porting from eslint + babel and have the following use case:
So in our render.js file we will have the following snippet:
But setting This means that we will never have an import/require in our tsx files. |
at this point I would recommend switching to the compiler options and disabling no-unused-variable, see #1481 and linked threads. |
Should be fixed by #2235, which uses TypeScript's implementation for |
I have react option enabled for "no-unused-variable"
Still React is reported as unused when imported like this:
but this import statement works
Example
It seems that the problem is caused by react-specific import detection code is only in visitNamespaceImport() method (noUnusedVariableRule.ts file), but not in visitImportEqualsDeclaration() method.
The text was updated successfully, but these errors were encountered: