简体   繁体   中英

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. So I cant use amd-dependency (since that works for amd modules only). And I also cannot manually declare the module since 1. it is autogenerated and 2. it has a relative path.

Typescript 1.6 currently shows an error saying it 'Cannot find module'. 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:

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

ie. just use var instead of import .

If you want to use TypeScript imports (which are just ES6 imports), you could use this:

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

and then call your API like this:

let foo = test.MY_API;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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