简体   繁体   English

如何拆分大型的typescript index.d.ts文件

[英]How to split large typescript index.d.ts file

Our application has many objects and such like that we can roughly break down into 3 categories; 我们的应用程序有许多对象,我们可以大致分为3类; 'core', 'app' and 'objects'. '核心','app'和'对象'。

I followed a basic Typescript tutorial online and currently there is a single index.d.ts which has this kind of layout: 我在网上跟踪了一个基本的Typescript教程,目前有一个index.d.ts,它有这种布局:

interface AH {

}

declare var ah: AH;

declare module 'ah' {
    export = ah;
}

declare module ah {

    module objects {
        // much objects
    }

    module app {
        //many app
    }

    module core {
        //such core
    }
}

However due to the archaic source control we use, it would be beneficial to break this single file out to 3 separate files for app, core and objects, having the "namespaces" available as ah.app.blah, ah.core.blah and ah.objects.blah. 但是由于我们使用了古老的源代码控制,将这个单个文件分解为3个单独的文件,包括应用程序,核心和对象,将“命名空间”作为ah.app.blah,ah.core.blah和ah.objects.blah。

How can I achieve this? 我怎样才能做到这一点?

If you are not using this library in another project, there is no need the declare modules. 如果您没有在另一个项目中使用此库,则不需要声明模块。

It seems like you have two options here: 看起来你有两个选择:

  1. Export the type declarations you want to expose in separate files, and import them when you need them. 导出要在单独文件中公开的类型声明,并在需要时导入它们。 As this seems to be the most common practice, also when you are handling external modules, I think this has my personal preference. 由于这似乎是最常见的做法,当您处理外部模块时,我认为这有我个人的偏好。

    app.d.ts (~ core.d.ts, objects.d.ts) app.d.ts (~core.d.d,objects.d.ts)

     export interface theAppInterface {} 

    src/index.ts (example usage) src / index.ts (示例用法)

     import { theAppInterface } from '../app' import { theCoreInterface } from '../core' let appUsage: ah.app.theAppInterface 
  2. Declare the separate categories in global declaration files, so they're thown into the global namespace and you don't have to import them separately everywhere. 在全局声明文件中声明单独的类别,因此它们被放入全局命名空间中,您不必在任何地方单独导入它们。 Eg. 例如。

    app.d.ts (~ core.d.ts, objects.d.ts) app.d.ts (~core.d.d,objects.d.ts)

     declare namespace ah { namespace app { interface theAppInterface {} } } 

    src/index.ts SRC / index.ts

     let appUsage: ah.app.theAppInterface let coreUsage: ah.core.theCoreInterface 

暂无
暂无

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

相关问题 如何在index.d.ts typescript定义文件中引用Redux类型? - How to reference Redux types in index.d.ts typescript definition file? index.d.ts 文件未发布到 NPM - index.d.ts file not published to NPM 如何从 index.d.ts 中 typescript 中的多个文件中导出同名的两个模块/命名空间? - How to export two modules/namespaces with same name from multiple files in typescript in index.d.ts? 是否为canonical-json提供index.d.ts文件? - Providing an index.d.ts file for canonical-json? 在源代码中使用 `index.d.ts` 声明文件 - Consume `index.d.ts` declaration file in source code 如何从 index.d.ts 文件手动导入一个类型,并告诉 VS Code 智能感知一个变量是那种类型? - How to manually import a type from an index.d.ts file, and tell VS Code intellisense that a variable is of that type? 从typescript模块自动生成index.d.ts,类型定义 - Auto generate index.d.ts, type definitions, from a typescript module 有没有办法编写一个定义标准js对象类型的index.d.ts声明文件类型? - Is there a way to write an index.d.ts declaration file type that defines a type of a standard js object? 组件无法从 React Native 中的 index.d.ts 文件自动导入类型 - Component fails to auto import types from index.d.ts file in React Native 包含`declare module 'graphql/error/GraphQLError' 的声明(.d.ts)文件; 在 angular 项目的 index.d.ts 中 - declaration (.d.ts) file containing `declare module 'graphql/error/GraphQLError'; in index.d.ts in an angular project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM