【问题标题】:Exporting Namespaces in ES6+在 ES6+ 中导出命名空间
【发布时间】:2020-07-25 12:06:17
【问题描述】:

我来自 Java 世界,我正在尝试创建一个具有 JSDOC 中记录的类型的 vanilla JS (ES2018) 应用程序,然后我使用 TypeScript 编译器来获取可以与我的应用程序捆绑的漂亮定义文件。我只有两个主要文件:client.js(包含一个默认导出的类)和 constants.js(一个带有一些常量的默认导出对象),它们都在 src/ 下。我想在一个共同的命名空间下公开这两个,所以我的 index.js 看起来像这样:

import XApiClient from 'src/brokers/xtb/x_api_client';
import {Constants} from 'src/brokers/xtb/x_api_constants';

/**
 * @namespace
 * @property {Constants} Constants
 * @property {XApiClient} XApiClient
 */
const XApi = {Constants: Constants, Client: XApiClient};
export default XApi;

整个项目随后通过 npm+git 在其他地方使用,但在这个新项目中,Typescript 编译器无法识别 XApi.XApiClient 的类型。我目前非常绝望,正在考虑放弃编码并去照顾山上的山羊。

【问题讨论】:

  • 您也需要该文件的定义文件。

标签: javascript typescript ecmascript-6 es6-modules


【解决方案1】:

最后,我只需要硬着头皮将所有内容迁移到 Typescript,最后我还需要解析 JSDOC 标记以获取定义文件的附加步骤。错误似乎是由于 tsconfig 上的错误配置造成的。

FWIW 这是我的 tsconfig(我使用 gts 作为基线,还请注意我使用 webpack 将所有内容打包到一个文件中):

{
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "outDir": "js/",
    "rootDir": "src/",
    "sourceMap": true,
    "moduleResolution": "node",
    "module": "commonjs",
    "target": "ES2018",
    "lib": ["dom","dom.iterable", "ES2018"],
    "noImplicitAny": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src/**/*.ts", "test/**/*.ts"],
  "exclude": ["dist", "node_modules"]
}

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 2017-11-04
    • 2017-07-25
    • 2023-03-13
    • 2015-12-21
    • 1970-01-01
    • 2019-05-26
    • 2015-03-17
    • 1970-01-01
    相关资源
    最近更新 更多