为什么Typescript编译器不会将.ts更改为.js? [英] Why does Typescript compiler not change .ts to .js?
问题描述
在文件夹左侧的下方图片中,您可以看到使用tsc编译到我的 / dist (紫色)Javascript中的 / src Typescript(蓝色)。 / p>
您可以在源文件中看到左边引用一个.ts模块文件,它没有被编译成引用右边的.js模块文件。
为什么不呢?如果tsc没有转换引用,Javascript怎么可能运行?
第二个问题:然后我尝试从 .ts 手动更改编译引用到 .js 并运行 node dist / server.js 但我收到错误找不到模块工具/ typescriptImport.js'。为什么节点在正确引用时无法找到模块(你可以在最右边看到它是一个模块)?
对于初学者,您必须从 import
中删除 .ts
扩展名。 TypeScript表示它将其视为静态字符串,不会更改它。
其次,根据经验,我猜使用 .d.ts
文件可以解决你的找不到模块
错误。我通过使用这个小黑客已经解决了很多次。您可以使用 ///< reference path =tools / typeScriptImports.d.ts/>
来引用它。想象一下 .d.ts
作为TypeScript的头文件。
最后,尝试相对于 server.js
文件创建路径。所以: ./ tools / typeScriptImports
。
In the image below on the left in the folders you can see my /src Typescript (blue) compiled into my /dist (purple) Javascript using tsc.
You can see in the source file on the left that references a .ts module file that it isn't compiled into referencing a .js module file on the right.
Why not? How could the Javascript possibly run if tsc doesn't convert references?
Second question: I then tried manually changing the compiled reference from .ts to .js and running node dist/server.js but I get the error cannot find module tools/typescriptImport.js'. Why does node fail to find the module when it is correctly referenced (and you can see on the far right that it is a module)?
For starters, you have to remove the .ts
extension from the import
. TypeScript says that it treats it as a static string and won't change it.
Second, out of experience, I guess using a .d.ts
file may solve your module not found
error. I have solved many times by using this small hack. You can reference it using /// <reference path="tools/typeScriptImports.d.ts" />
. Imagine .d.ts
as the header file for TypeScript.
Lastly, try and make the path relative to the server.js
file. So: ./tools/typeScriptImports
.
这篇关于为什么Typescript编译器不会将.ts更改为.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!