简体   繁体   English

如何在打字稿中组织我的类型/接口声明?

[英]How do I organise my types/interfaces declarations in typescript?

I'm a bit new in typescript and one of the things that makes me a little confused are type/interface declarations.我在打字稿方面有点新,让我有点困惑的一件事是类型/接口声明。 I'm writing a lib that currently has a few classes and a few more types/interfaces declarations.我正在编写一个当前有几个类和更多类型/接口声明的库。 I use these types/interfaces throughout most of the code, initially I would leave them in a single file in a types folder, then I would import them wherever needed, now I have a single models.d.ts inside the types folder that declares a namespace types , inside this namespace I export all my types/interfaces that I'm using throughout the code.我使用这些类型/接口最全的代码,首先我会离开他们在一个单一的文件types文件夹,然后我将它们导入任何需要的地方,现在我有一个单一的models.d.ts类型的文件夹里面声明namespace types ,在这个命名空间内,我导出我在整个代码中使用的所有类型/接口。 Is this approach OK or is there a better way for organising types and interfaces definitions?这种方法可以吗,还是有更好的方法来组织类型和接口定义?

Normally you only use the .d.ts files for type definition files for JS libraries that are not strongly typed.通常,您只将 .d.ts 文件用于非强类型的 JS 库的类型定义文件。 Here is another SO about .d.ts files: About "*.d.ts" in TypeScript .这是关于 .d.ts 文件的另一个 SO: 关于 TypeScript 中的“*.d.ts” Having a directory with your types is the solution we use and seems to work well.拥有一个包含您的类型的目录是我们使用的解决方案,并且似乎运行良好。 Yes, you have to import types in files, but it allows you to be explicit about it and if you use the "TypeScript Hero" plugin VS Code can automatically add the imports for you.是的,您必须在文件中导入类型,但它允许您明确说明它,如果您使用“TypeScript Hero”插件,VS Code 可以自动为您添加导入。

Also, you can put multiple types in a single file, but I would do so only with grouped together functionality, such as if you airplanes, cars and boats and if they are small enough, such as the file vehicles.ts , or you could have four files: vehicles.ts , airplanes.ts , cars.ts and boats.ts , where you have the generic types in vehicles.ts and the specific ones in the other files.此外,您可以将多种类型放在一个文件中,但我只会使用组合在一起的功能来这样做,例如,如果您是飞机、汽车和船只,并且它们足够小,例如文件vehicles.ts ,或者您也可以有四个文件: vehicles.tsairplanes.tscars.tsboats.ts ,在那里你必须在泛型类型vehicles.ts而在其他文件中的特定的人。

Now if you started with a single file, vehicles.ts that began to grow with a bunch of different types, and decided you wanted to break them out, one way you could do that with no refactoring to anything but your type code is create a directory called vehicles, then put all the files in their that you refactor, and end with an index.ts file that contains imports and exports for all of the types.现在,如果你开始一个文件, vehicles.ts即开始与一群不同类型的增长,决定了你想打破他们,你能做到这一点,没有重构,以任何一种方式,但你的类型代码是创建一个名为车辆的目录,然后将所有文件放入您重构的文件中,并以包含所有类型的导入和导出的 index.ts 文件结尾。 Here's an example:下面是一个例子:

Your project file:您的项目文件:

import { Boeing737, Boeing747, FordExplorer, BassBoat } from 'src/types/vehicle'

This is currently referencing the vehicles.ts file that is in that directory.这是当前引用该目录中的 Vehicle.ts 文件。 Now once we do the refactoring, and now have the directory: 'src/types/vehicle' which has the refactored .ts files under it you create an index.ts file that looks like this:现在,一旦我们进行了重构,并且现在有了目录:'src/types/vehicle',其中包含重构的 .ts 文件,您将创建一个index.ts文件:

export { Boeing737, Boeing747 } from './airplanes';
export { FordExplorer, ToyotaCorolla, ToyotaSienna } from './cars';
export { BassBoat, Yacht } from './boats';

Now your import in your project file doesn't change at all but it is now getting its types from the sub files.现在您在项目文件中的导入根本没有改变,但它现在从子文件中获取其类型。 This then allows for further refactoring without changing your project files, such as if you wanted to separate out cars from SUVs.这样就可以在不更改项目文件的情况下进行进一步重构,例如,如果您想将汽车与 SUV 分开。 If you are using the same types in all the files, you just need the one import line that you could copy to new files, or just import them as needed.如果您在所有文件中使用相同的类型,您只需要一个可以复制到新文件的导入行,或者根据需要导入它们。

If you want to read more about index.ts files and how they work, here is the Typescript documentation: https://www.typescriptlang.org/docs/handbook/module-resolution.html .如果您想了解有关 index.ts 文件及其工作原理的更多信息,请参阅 Typescript 文档: https ://www.typescriptlang.org/docs/handbook/module-resolution.html。

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

相关问题 如何将我的 nextjs typescript(接口/类型)与 nodejs 服务器 typescript 模型(接口/类型)一起保存? - How I can keep my nextjs typescript (interfaces / types) with nodejs server typescript models (interfaces / types)? 如何正确声明以下打字稿接口/类型? - How do I declare the following typescript interfaces/types correctly? 如何将其他类型导入TypeScript自定义声明文件? - How to import other types into my TypeScript custom declarations file? 将程序包发布到NPM时,如何包含额外的Typescript声明? - How do I include extra Typescript declarations when I publish my package to NPM? 如何在打字稿中为我的Chrome扩展代码找到@types? - How do I find @types for my chrome extension code in typescript? 如何在TypeScript中创建Ambient类声明 - How do I create Ambient Class Declarations in TypeScript Typescript 动态类型声明 - Typescript dynamic types declarations 我可以为 typescript 接口分配 2 种不同的类型吗? - Can I assign 2 different types to typescript interfaces? 我如何在我的 React 类中的接口中包含接口? - How do I include Interfaces within Interfaces In my React Class? 如何通过@types为scoped / namespaced包安装TypeScript声明? - How can I install TypeScript declarations for scoped/namespaced packages via @types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM