【问题标题】:Force typescript not to remove unused references强制打字稿不删除未使用的引用
【发布时间】:2016-03-22 19:55:28
【问题描述】:

请在 github 上查看我的demo project

在我的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 从未使用过,但react-addons-test-utils 需要(如果没有 React 导入,我会收到错误消息“React 未定义”)。问题是:没有线

var R = React;

此导入已被 typescript 编译器删除。

import 'react'

不会被删除但不起作用(同样的错误消息“React 未定义”)。

如何在不创建虚拟变量的情况下解决它?

运行

npm install
tsd install
npm run test 

检查。

【问题讨论】:

    标签: javascript unit-testing import reactjs typescript


    【解决方案1】:

    老实说,我不明白为什么会发生这种情况,但请尝试添加以下行:

    global.React = React
    

    甚至这可能会起作用:

    React;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-21
      • 2021-01-23
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2021-11-18
      相关资源
      最近更新 更多