强制typescript不要删除未使用的引用

Force typescript not to remove unused references

本文关键字:未使用 引用 删除 typescript 强制      更新时间:2023-09-26

请查看我在github上的演示项目。

在我的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导入从未使用过,但react-addons-test-utils需要它(在没有React导入的情况下,我收到一条错误消息"React is not defined")。问题是:没有线路

var R = React;

typescript编译器将删除此导入。

import 'react'

没有被删除,但不起作用(相同的错误消息"React is not defined")。

如何在不创建伪变量的情况下处理它?

运行

npm install
tsd install
npm run test 

检查。

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

global.React = React

甚至这也可能奏效:

React;