如何组织 TypeScript 接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36633033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:03:55  来源:igfitidea点击:

How to organize TypeScript interfaces

typescript

提问by gevik

I would like to know what the recommended way is to organize interface definitions in typescript. In my project I have many different classes. Each class can have a configuration interface. Currently I have gathered all of these interface definitions into one file Interfaces.tsand reference the file from various locations.

我想知道在打字稿中组织接口定义的推荐方法是什么。在我的项目中,我有许多不同的课程。每个类都可以有一个配置接口。目前,我已将所有这些接口定义收集到一个文件中,Interfaces.ts并从不同位置引用该文件。

Is there a better way?

有没有更好的办法?

Example: https://github.com/blendsdk/blendjs/blob/devel/blend/src/common/Interfaces.ts

示例:https: //github.com/blendsdk/blendjs/blob/devel/blend/src/common/Interfaces.ts

回答by basarat

Global

全球的

The TypeScript team uses a file called types.ts: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts

TypeScript 团队使用一个名为types.tshttps: //github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts的文件

I do the same in my project e.g. https://github.com/alm-tools/alm/blob/master/src/common/types.ts

我在我的项目中做同样的事情,例如https://github.com/alm-tools/alm/blob/master/src/common/types.ts

Alternative

选择

Its okay for highreuse portions to belong in types, however don't pollute it with specific types. Specific types can be (should be) closer to their reference e.g. react style props belong next to (in the same file as) the react component.

其好于复用的部分属于中types,但不与特定类型的污染它。特定类型可以(应该)更接近它们的引用,例如反应样式道具属于反应组件旁边(在同一文件中)。

React Component Prop Type/ React Component

反应组件道具类型/反应组件

回答by Thomas Harlan

I like to structure my apps like this:

我喜欢这样构建我的应用程序:

|-- app
    |-- components
    |-- directives
    |-- services
    |-- interfaces
        |-- <feature/component name>
            |-- <interface name>.interface.ts

回答by pouya

My projects are constructed like this

我的项目是这样构建的

src/
└── ts
    ├── enums
    │   ├── app_enums.ts
    │   └── db_enums.ts
    ├── interfaces
    │   ├── app_interfaces.ts
    │   ├── db_interfaces.ts
    │   └── global_interfaces.ts
    └── types
        └── app_types.ts