简体   繁体   English

如何在许多Visual Studio(2013)项目中处理TypeScript类型定义?

[英]How do I handle TypeScript type definitions across many Visual Studio (2013) projects?

I develop quite a large application that consist of a lot of javascript projects (VS projects). 我开发了相当大的应用程序,其中包含许多javascript项目(VS项目)。 Now, I'm considering to convert the JS step by step to TypeScript. 现在,我正在考虑将JS逐步转换为TypeScript。

I use a number of external libraries, such as JQuery, Knockout Js, etc and downloaded their d.ts files to one of the projects. 我使用了许多外部库,例如JQuery,Knockout Js等,并将其d.ts文件下载到一个项目中。 Each of my project in turn will have to generate its own d.ts file. 反过来,我的每个项目都必须生成自己的d.ts文件。 I found the "generate source map" setting in VS, so I got that one. 我在VS中找到了“生成源地图”设置,所以我知道了。

I realize that I pretty soon will have a huge amount of d.ts files that would need to be included in almost all projects. 我意识到很快我将拥有几乎所有项目中都需要包含的大量d.ts文件。 I can beforehand see that this will be a huge problem. 我可以事先看到这将是一个巨大的问题。 I googled around a bit but maybe I'm searching it wrong. 我在Google上搜索了一下,但也许搜索错了。 Cannot find a good approach on this. 在此上找不到好的方法。

As I see it, the best solution would be if all projects got their type definitions from some kind of common type definitions folder. 如我所见,最好的解决方案是,如果所有项目都从某种通用类型定义文件夹中获得其类型定义。 Is that somehow possible? 有可能吗?

First, remember that .d.ts files (hereafter type declaration files) are only for type definitions for JavaScript code. 首先,请记住.d.ts文件(以下称为类型声明文件)仅用于JavaScript代码的类型定义。 If you're writing your code in TypeScript, there will be no declaration files. 如果您使用TypeScript编写代码,则将没有声明文件。 There will only be .ts. 只有.ts。

In a pure TypeScript project, you'll typically have all your code in TypeScript, and the only type declaration files you'll have are the 3rd party dependencies, eg jquery.d.ts, bootstrap.d.ts, etc. 在纯TypeScript项目中,通常会将所有代码都存储在TypeScript中,唯一的类型声明文件就是第三方依赖关系,例如jquery.d.ts,bootstrap.d.ts等。

If your project is pure TypeScript, I'd manage all those type declaration files just using NuGet. 如果您的项目是纯TypeScript,我将仅使用NuGet来管理所有这些类型声明文件。 Install your dependencies using NuGet, and install the type declaration files using NuGet. 使用NuGet安装依赖项,并使用NuGet安装类型声明文件。 NuGet can then manage dependencies for your entire solution. NuGet然后可以管理整个解决方案的依赖性。

If your project is not pure TypeScript, that is, you're converting it piece at a time, and you create type declaration files for your JavaScript code that is not yet ported to TypeScript, then I'd suggest adding your type declarations in a single file and linking that file into all the projects that need it. 如果您的项目不是纯TypeScript,也就是说,您一次要对其进行转换,并且为尚未移植到TypeScript的JavaScript代码创建类型声明文件,那么建议您将类型声明添加到单个文件,然后将该文件链接到需要它的所有项目中。 Then you have a single place for all your type declarations. 然后,所有类型声明都放在一个位置。

One other note: source maps and .d.ts files have nothing to do with each other. 另一个说明:源映射和.d.ts文件彼此无关。 Source maps are JSON files that map TS line of code to JS line of code. 源映射是将TS代码行映射到JS代码行的JSON文件。 This effectively lets you debug your TypeScript in the browser, if you so choose. 如果您愿意,这可以有效地使您在浏览器中调试TypeScript。

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

相关问题 Visual Studio代码-MongoDB的TypeScript定义 - Visual Studio Code - TypeScript Definitions for MongoDB 如何在Visual Studio 2013中设置打字稿开发工作流程? - How to setup typescript development workflow in visual studio 2013? Visual Studio 2013环境声明中的TypeScript - TypeScript in Visual Studio 2013 Ambient Declarations 如何为TypeScript使用AWS sdk定义? - How do I use AWS sdk definitions for TypeScript? 在Visual Studio 2013中,如何在构建后步骤中缩小Javascript和CSS - In Visual Studio 2013, how do I minify Javascript and CSS in the post-build step 我需要如何更改这些 TypeScript mixin 类型定义,以便允许定义允许 class 扩展特征的 mixin? - How do I need to change these TypeScript mixin type definitions in order to allow the definition of mixins that allow a class to extend the trait? 如何在 Visual Studio 2012/2013 中为 JS 进行区域折叠 - How to do region collapse for JS in Visual Studio 2012/2013 如何在 TypeScript 项目中重用现有的 C# 类定义 - How to reuse existing C# class definitions in TypeScript projects TypeScript 类型定义:如何声明 class 构造函数 - TypeScript type definitions: how to declare class constructors 如何使JavaScript解析在Visual Studio 2013中工作 - How can I get javascript parsing to work in Visual Studio 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM