简体   繁体   English

强制打字稿不删除未使用的引用

[英]Force typescript not to remove unused references

Please check my demo project on github. 请检查我在github上的演示项目

In my react-tfractal/tests/component/tfractal.spec.tsx test: 在我的react-tfractal / tests / component / tfractal.spec.tsx测试中:

import * as React from 'react';
import * as ReactTestUtils from 'react-addons-test-utils';
import {expect} from 'chai';
import {Tfractal} from '../../src/tfractal';

var R = React;

describe('tfractal', function () {
  it('renders', function () {
    var tfractal = ReactTestUtils.renderIntoDocument(<Tfractal />);
    expect(tfractal).exist;
  });
}); 

React import is never used, but required by the react-addons-test-utils (I get an error message "React is not defined" without the React import). 从不使用React导入,但是react-addons-test-utils要求使用React导入(如果没有React导入,我会收到一条错误消息“未定义反应”)。 The problem is: without the line 问题是:没有线

var R = React;

this import is removed by the typescript compiler. 此导入由打字稿编译器删除。

The

import 'react'

doesn't get removed but doesn't work (the same error message "React is not defined"). 不会被删除但无法正常工作(相同的错误消息“未定义反应”)。

How can I work around it without creating a dummy variable? 如何在不创建虚拟变量的情况下解决该问题?

Run

npm install
tsd install
npm run test 

to check. 去检查。

I don't understand why this is happening to be honest, but try adding this line: 我不明白为什么会这样,但是尝试添加以下内容:

global.React = React

Or even this might work: 甚至这可能有效:

React;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM