简体   繁体   English

TypeScript类型定义最佳实践

[英]TypeScript type definition best practices

I have read through dozens of pages trying to figure out the best way to setup my type definitions in TypeScript. 我已阅读了几十页,试图找出在TypeScript中设置我的类型定义的最佳方法。

  • I used to have a typings.ts file somewhere in my project and would then import the types in each and every file they are needed, by doing something like 我曾经在我的项目中的某个地方有一个typings.ts文件,然后通过做类似的事情,在每个需要的文件中导入类型

import {IMyCustomType} from './typings';

and inside my typings file I would declare my types like: 在我的打字文件中,我会声明我的类型:

export interface IMyCustomType {...}

Instead of using export interface IMyCustomType {..} they use declare interface IMyCustomType {..} 他们使用declare interface IMyCustomType {..}而不是使用export interface IMyCustomType {..} declare interface IMyCustomType {..}

This setup has one big advantage for me: I don't need to explicitly import the types in each file and the interfaces are available within the entire project directly. 这个设置对我来说有一个很大的优势:我不需要在每个文件中显式导入类型,并且接口在整个项目中直接可用。

Questions: 问题:

1) Is it correct that all **/*.d.ts files will be automatically imported during the compilation ? 1)编译期间是否会自动导入所有**/*.d.ts文件是否正确?

2) Is it a good practice to use declare and make all types available to the entire project ? 2)使用declare并使所有类型可用于整个项目是一种好的做法吗?

3) Is there a standard directory path and name where I should put my type definitions ? 3)是否有标准的目录路径和名称,我应该在哪里放置我的类型定义?

Basically I am trying to make my global interfaces automatically available everywhere in my project without having to import them explicitely. 基本上我试图使我的全局接口在我的项目中随处可用,而不必明确地导入它们。 Is this something I should do and how do I setup my project to achieve this ? 这是我应该做的事情,我如何设置我的项目来实现这一目标?

UPDATE UPDATE

After raising this with my team, most were against having ambient types, so we decided to import types whenever needed. 在我的团队提出这个问题之后,大多数人反对使用环境类型,因此我们决定在需要时导入类型。 To make this easier we are relying on our IDEs to automatically import said types. 为了使这更容易,我们依靠我们的IDE自动导入所述类型。

Since Typescript 2, you should be using d.ts files. 从Typescript 2开始,你应该使用d.ts文件。 With this approach you reduce a lot of your config files. 使用这种方法可以减少很多配置文件。

You can find more about it in: 您可以在以下位置找到更多相关信息

You should use the @types NPM scope. 您应该使用@types NPM范围。 You can find a more detailed guide in the Typescript Handbook , but basically all you need to do is run an NPM command: 您可以在Typescript手册中找到更详细的指南,但基本上您需要做的就是运行NPM命令:

npm install --save @types/lodash

About your questions, basically by using this method you don't need to worry about type definition files at all, their existence for third party libraries is transparent to you (you don't care where they are placed or how to import them, you only need to import the modules themselves). 关于你的问题,基本上使用这种方法你根本不需要担心类型定义文件,它们对第三方库的存在对你来说是透明的(你不关心它们的放置位置或如何导入它们,你只需要自己导入模块)。

About using declare , I think that's a separate topic, but you can check out this question: What's the difference between "declare class" and "interface" in TypeScript 关于使用declare ,我认为这是一个单独的主题,但你可以看看这个问题: TypeScript中“declare class”和“interface”之间的区别是什么

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

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