我正在构建一个依赖PDFKit的网络应用。 PDFKit可以使用Webpack在浏览器中运行。由于我正在使用带有Webpack的Typescript,因此我将包含@types/node定义,以便我的PDFKit接口正常工作。
这导致应用程序的其他部分利用浏览器全局(例如Timer)或Webpack运行时全局(例如require),其中打字与Node不同,以抛出错误。
是否有人想要一种干净的方式将Node输入隔离到必要的文件?过去我根据需要分散import * as request from 'supertest';
import * as td from 'testdouble';
import { server } from '../../server';
import { finishTest } from '../../spec/helpers/suptertest';
describe('user route', function () {
let app: any;
beforeEach(function () {
app = server;
});
afterEach(function (done) {
app.close(done);
});
it('creates a user /', (done) => {
//make request
request(app)
.post('/api/user')
.send({
firstName: 'Philippe',
lastName: 'Vaillancourt',
city: 'Laval',
state: 'Qc',
password: 'test',
email: 'test@test.com',
})
.expect(201, finishTest(done));
});
});
- 例如:< / p>
any
那只是丑陋而且不可持续。还有更好的方法吗?
答案 0 :(得分:0)
作为一种解决方法,您可以从节点中禁用require
输入。
在typeRoots
中明确设置tsconfig.json
。在这种情况下,@types
文件夹将不再用于查找类型。在自定义类型根目录中,您可以创建index.d.ts文件,其中// @引用来自@types的typyngs(node.d.ts除外)。您还可以在自定义输入根目录中提供已修改的node.d.ts自定义版本。
不是最佳解决方案。缺点很明显:您必须以某种方式维护引用和节点类型的自定义版本:)
(有关typeRoots的信息,请参阅http://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
更新
此处还有一个答案: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11324#issuecomment-265046822
您可以为每组文件创建单独的tsconfig.json文件,这些文件使用节点,但不是。在那里,您可以明确选择要在组中应用的打字。 您仍然需要维护一组打字名称,但它要小得多。