简体   繁体   English

如何在没有模块声明的情况下导入Typescript

[英]How to import in Typescript without module declaration

I have a bunch of autogenerated modules that i need to reference from my typescript files. 我有一堆自动生成的模块,我需要从我的打字稿文件中引用。

Eg 例如

import test = require('../templates/test')

I am generating CommonJS modules with ES5 output. 我正在使用ES5输出生成CommonJS模块。 So I cant use amd-dependency (since that works for amd modules only). 所以我不能使用amd-dependency (因为它仅适用于amd模块)。 And I also cannot manually declare the module since 1. it is autogenerated and 2. it has a relative path. 而且我也无法手动声明模块,因为1.它是自动生成的,2。它有一个相对路径。

Typescript 1.6 currently shows an error saying it 'Cannot find module'. Typescript 1.6当前显示错误,说“无法找到模块”。 How do i make it suppress this error and import? 如何使它抑制此错误并导入?

How do i make it suppress this error and import 如何使其抑制此错误并导入

If you are sure that the require statement is valid and want to switch off any type checking on the import, you can just use node.d.ts and do: 如果您确定require语句有效并且想要关闭导入的任何类型检查 ,则可以使用node.d.ts并执行:

var test = require('../templates/test')

ie. 即。 just use var instead of import . 只需使用var而不是import

If you want to use TypeScript imports (which are just ES6 imports), you could use this: 如果要使用TypeScript导入(只是ES6导入),可以使用:

import * as test from '../templates/test';

and then call your API like this: 然后像这样调用你的API:

let foo = test.MY_API;

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

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