如何维护typescript(.ts)和它的javascript(.js)之间的关系?

pab*_*ndo 2 typescript

我有一个打字稿项目工作正常.我创建了第二个项目,并通过文件系统复制了我以前的一些代码.除了我的.js现在与我的.ts不同步之外,一切正常.我现在如何解决问题:删除两个文件并通过拖动ts文件(将自动携带.js)再次使用Visual Studio界面复制 - 无论如何,我很好奇,现在我想知道,.ts是怎么回事并且.js互相认识?在ASP.NET中你有背后的代码概念.但是在这里我看不出这是怎么做的.Visual Studio是否将关系保存在隐藏文件中?

Phi*_*ayr 5

在第75页的打字稿语言规范(第9.1章)中:

默认情况下,会为编译中的每个实现源文件生成JavaScript输出文件,但不会从声明源文件生成输出.

There is no real association between ts and js files other than the name. A ts file is compiled to a js file and at runtime the js files are loaded. Note that the actual naming of the js files is independent of the module names and you are responsible yourself to load the scripts accordingly (e.g. using <script> in your html page).

In your case it should be fine to just copy and add a reference to the ts files. After you add the reference, be sure to change the Build Action to "TypeScriptCompile" to make sure it is passed to the typescript compiler: Select the file in solution explorer, press F4 to bring up the properties window and change the Build Action.

Update: To make the files "nested", as mentioned in the comment, you can unload the project and add a child element to the js element, e.g. transforming

<Content Include="some.js" />
<TypeScriptCompile Include="some.ts" />
Run Code Online (Sandbox Code Playgroud)

to

<Content Include="some.js">
    <DependentUpon>some.ts</DependentUpon>
</Content>
<TypeScriptCompile Include="some.ts" />
Run Code Online (Sandbox Code Playgroud)