简体   繁体   English

模块导出对象声明

[英]Declaration for Module Exporting Object

I have a project that compiles and exposes two CommonJS modules; 我有一个编译并公开两个CommonJS模块的项目。 each of them an object. 他们每个人一个对象。 Then, I have another project that consumes them. 然后,我有另一个消耗它们的项目。

The project structures are as follows: 项目结构如下:

multi-mod-package
|-mod1
| |-index.js
|-mod2
| |-index.js
|-package.json
multi-mod-consumer
|-index.ts
|-package.json
|-tsconfig.json

And the file contents: 以及文件内容:

multi-mod-package/mod1/index.js multi-mod-package / mod1 / index.js

module.exports.default = {
    "count": "One"
};

multi-mod-package/mod2/index.js multi-mod-package / mod2 / index.js

module.exports.default = {
    "count": "Two"
};

multi-mod-consumer/index.ts 多种消费/ index.ts

import one  from "multi-mod-package/mod1";
import two  from "multi-mod-package/mod2";

console.log(`${one.count}, ${two.count}, Buckle My Shoes`);

multi-mod-consumer/tsconfig.json multi-mod-consumer / tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true
    },
    "include": [
        "index.ts"
    ]
}

Without the noImplicitAny , this works fine. 没有noImplicitAny ,这可以正常工作。 The project compiles, and One, Two, Buckle My Shoes is output as expected. 编译该项目,并按预期输出“ One, Two, Buckle My Shoes However, the modules have no typings. 但是,这些模块没有类型。 I would like to enable noImplicitAny and have proper typings for these modules, so I am trying to write declaration files for each. 我想启用noImplicitAny并为这些模块输入正确的类型,因此我试图为每个模块编写声明文件。 I have tried a number of iterations of declare module and declare namespace , but I keep getting "... is not a module" errors when attempting to consume the modules. 我尝试了多次declare moduledeclare namespace的迭代,但是在尝试使用模块时,我不断收到“ ...不是模块”错误。

How do I write a declaration file for a module that exports an object? 如何为导出对象的模块编写声明文件?

UPDATE 更新

Adding two definition files - multi-mod-package/mod1/index.d.ts and multi-mod-package/mod1/index.d.ts - with the contents declare module "multi-mod-package/mod1"; 添加两个定义文件multi-mod-package/mod1/index.d.tsmulti-mod-package/mod1/index.d.ts内容declare module "multi-mod-package/mod1"; and declare module "multi-mod-package/mod1"; declare module "multi-mod-package/mod1"; - respectively - gets rid of the "... is not a module" errors. -分别-摆脱“ ...不是模块”错误。 However, there is still no proper typings; 但是,仍然没有正确的输入方式。 one.count still comes up as type any . one.count仍然显示为any类型。

When I try to give the module a body (even just an empty one), TypeScript complains that the module has no default export. 当我尝试给模块一个主体(甚至只是一个空的主体)时,TypeScript抱怨该模块没有默认导出。 So, I still need a solution for telling TypeScript the module is an object containing the count property. 因此,我仍然需要一种告诉TypeScript模块是包含count属性的对象的解决方案。

You need to declare the sub-module as individual modules. 您需要将子模块声明为单独的模块。

In your typings (eg typings/multi-mod-package.d.ts ): 在您的输入内容中(例如, typings/multi-mod-package.d.ts ):

declare module 'multi-mod-package/mod1' {
   // typings for mod1
}

declare module 'multi-mod-package/mod2' {
   // typings for mod2
}

For how to write your typings, you can check out the handbook: 有关如何输入内容的信息,您可以查看手册:

https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

TypeScript does not do automatic deeplinking because node resolution for deep linking can be different than the typings structure. TypeScript不会自动进行深层链接,因为用于深层链接的节点分辨率可能与类型结构不同。

Node deep linking is resolved based on the location of package.json. 节点深层链接是根据package.json的位置解析的。

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

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