简体   繁体   English

TypeScript:导入的模块未编译

[英]TypeScript: Imported Module Not Compiling

So I have a simple TypeScript file that imports a local TypeScript module. 因此,我有一个简单的TypeScript文件,该文件可导入本地TypeScript模块。 Everything links fine when I compile it, but the compiled JavaScript file is trying to require my TypeScript module which I'm importing, instead of compiling it. 当我编译它时,所有链接都可以正常运行,但是已编译的JavaScript文件正试图require我要导入的TypeScript模块,而不是对其进行编译。

Here's an example of my file layout: 这是我的文件布局的示例:

module.ts module.ts

declare module "MyModule" {
  export var name: string;
}

test.ts 测试

/// <reference path="../../src/module.ts"/>

import MyModule = require('MyModule');

var myName = MyModule.name;
myName = 'Nick';

console.log(myName);

I attempt to compile it like so: 我尝试这样编译它:

ntsc mockup.ts and also tried (with the same results) ntsc mockup.ts --module commonjs ntsc mockup.ts并且还尝试了(结果相同) ntsc mockup.ts --module commonjs

It compiles to this: 它编译为:

test.js test.js

/// <reference path="../../src/modern/needle.ts"/>
"use strict";
var MyModule = require('MyModule');
var myName = MyModule.name;
myName = 'Nick';
console.log(myName);

It also attempts to compile module.ts but the file that comes from it is completely empty. 它还尝试编译module.ts但来自它的文件完全为空。 Can anyone help me figure out what's going wrong? 谁能帮助我找出问题所在? The line var MyModule = require('MyModule'); 这行var MyModule = require('MyModule'); clearly isn't correct (the file wrong run because of it). 显然是不正确的(因为文件运行错误)。 I suspect something might be wrong with my module file, especially since it's not compiling correctly. 我怀疑模块文件可能出了问题,尤其是因为它无法正确编译。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Your file layout should look something like this: 您的文件布局应如下所示:

test.ts 测试

import mod = require('./MyModule');

console.log(mod.name);

MyModule.ts 我的模块

export var name = 'Hello, world!';

Run tsc --module commonjs test.ts to compile, then node test.js to execute. 运行tsc --module commonjs test.ts进行编译,然后运行node test.js进行执行。

It also attempts to compile module.ts but the file that comes from it is completely empty 它还尝试编译module.ts,但来自它的文件完全为空

Because module.ts only has declarations . 因为module.ts只有声明 Declarations are just hints to the compiler and will not do any actual JavaScript emit. 声明只是对编译器的提示,不会发出任何实际的JavaScript。

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

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