简体   繁体   English

typescript:使用typescript项目中的声明文件全局暴露类型

[英]typescript: using declaration files in a typescript project to expose types globally

I have recently started working with typescript. I have a typescript project setup which has a lot of React components written with typescript. These typescript components usually export the component itself and to make development convenient, I have extracted all the types required by this typescript component to a separate file.我最近开始使用 typescript。我有一个 typescript 项目设置,其中有很多用 typescript 编写的 React 组件。这些 typescript 组件通常导出组件本身,为了方便开发,我提取了这个 typescript 组件所需的所有类型到一个单独的文件。 Now usually you would have to import these types in your component to use it, but since it was cumbersome, some one in the team came up with an idea to name these separate file containing types as types.d.ts (or whatever.d.ts).现在通常你必须在你的组件中导入这些类型才能使用它,但由于它很麻烦,团队中的一些人想出了一个想法,将这些包含类型的单独文件命名为types.d.ts (或 whatever.d .ts)。 This makes the types available globally across the project, without needing to explicitly import the types anywhere.这使得类型在整个项目中全局可用,而无需在任何地方显式导入类型。

Now, I understand that type declaration files are only required in case I am consuming a javascript component in my typescript project, and if I want to have type checking working for that javascript component.现在,我知道只有在我的 typescript 项目中使用 javascript 组件,并且我希望对该 javascript 组件进行类型检查时,才需要类型声明文件。 In which case, I can either get the type declaration file from outside (like, DefinitelyTyped), or I can create a local declaration file.在这种情况下,我可以从外部获取类型声明文件(如 DefinitelyTyped),或者我可以创建一个本地声明文件。 This is not what i am using a declaration file here for.这不是我在这里使用声明文件的目的。

But i would like to understand what are the problems with this usage?但我想了解这种用法有什么问题?

  • Is it wrong to expose all the types globally (Please note that most of these types are only required internally by the component, only some are required by other components which consume this component)?全局公开所有类型是否错误(请注意,这些类型中的大多数仅在组件内部需要,使用该组件的其他组件仅需要一些类型)?
  • Is there going to be any problem with the compiler or the typescript setup due to this?编译器或 typescript 设置是否会因此出现问题?
  • I have read about a compiler option which generates the declaration files, can this setup interfere with that?我读过有关生成声明文件的编译器选项,此设置会干扰它吗?

NOTE: This separate file which is named as d.ts does not really follow any specific guidelines that may be required for declaration files.注意:这个名为 d.ts 的单独文件并不真正遵循声明文件可能需要的任何特定指南。 This just has some regular interface declarations这只是一些常规的接口声明

Some observations:一些观察:

  • in a .ts file (or a d.ts file), if there is no top level export, it would be considered a global script (similar to how this works in javascript) and everything in the script would be available globally..ts文件(或d.ts文件)中,如果没有顶级导出,它将被视为全局脚本(类似于它在 javascript 中的工作方式)并且脚本中的所有内容都将在全局可用。 This is how the types are exposed globally in my case (again, it does not have to be a .d.ts file)在我的案例中,这就是类型在全球范围内公开的方式(同样,它不必是.d.ts文件)

  • if you a ts file and a d.ts file with the same name, the compiler ignores the d.ts file and that is why it is not possible to expose types for Component.tsx using Component.d.ts (you would need to name the d.ts file something else)如果你有一个ts文件和一个同名的d.ts文件,编译器会忽略d.ts文件,这就是为什么不能使用Component.d.ts公开Component.tsx类型的原因(你需要将d.ts文件命名为其他名称)

  • when importing an external module without type information, typescript suggests to add a d.ts file with declare module moduleName , instead of this you can also just add a regular.d.ts file with a top level export inside moduleName/index.d.ts当导入没有类型信息的外部模块时,typescript 建议添加一个带有declare module moduleNamed.ts文件,除此之外,您还可以只添加一个 regular.d.ts 文件,并在moduleName/index.d.ts

So, to answer my questions:所以,回答我的问题:

  • it might not be wrong to expose all the types globally used within your application, but then you have to take care of any namespacing/naming conflicts.公开应用程序中全局使用的所有类型可能并没有错,但是您必须处理任何命名空间/命名冲突。 In this case, using d.ts file is not required, this setup would just work as well with a .ts file exposing the types globally.在这种情况下,不需要使用d.ts文件,此设置与.ts文件一起工作也可以在全局范围内公开类型。 Another way to achieve this would be to export types from respective files and import them wherever required.实现此目的的另一种方法是从各自的文件中导出类型并在需要时导入它们。

  • in this setup, the only problem with the compiler which may arise is when the .d.ts file is named same as the .ts file, in which case the .ts file would override and types from .d.ts would not be available globally.在此设置中,编译器可能出现的唯一问题是当.d.ts文件的名称与.ts文件相同时,在这种情况下, .ts文件将覆盖并且.d.ts中的类型将不可用全球范围内。

  • the compiler generates the declaration files from ts files, and that is mostly required if you need to publish your types.编译器从ts文件生成声明文件,如果你需要发布你的类型,那是最需要的。 In our case, the types are internal and we won't really need to generate the declarations.在我们的例子中,类型是内部的,我们实际上不需要生成声明。 Our usage, as discussed, is not dependent on the file being a declaration file, it will work just as well in a .ts file and we shouldn't be using .d.ts file for this.正如所讨论的,我们的用法不依赖于文件是声明文件,它在.ts文件中也能正常工作,我们不应该为此使用.d.ts文件。 So, no, there wouldn't be any impact.所以,不,不会有任何影响。

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

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