如何整理打字稿类型文件?

时间:2019-07-08 19:23:43

标签: node.js typescript webpack types

有什么方法可以整理或调试types文件?

我有一个所有类型的index.d.ts文件,但似乎在启动时使我的应用程序崩溃了。

使用tsc和一些基本选项可以正常工作 使用tslint可以正常工作。 但是当使用webpack编译器运行时,出现以下错误。

文件内容很多,但基本上如下所示。 在导出枚举之前,我遇到了一些问题,但基本上没有得到我能理解的任何实际错误。

// we SEND to Boto
export enum MsgTypeOut {
  TEXT = 0,
  IMAGE = 1,
  URL_LINK = 2,
  FILE = 3,
}

export interface BotoTextMsg {
  chatId: string
  messageType: MsgTypeOut.TEXT
  token?: string
  payload: {
    text: string
  }
}
  nodemon:watch early exit on watch, still watching (1) +14s
/Users/dc/dev/tix/recobot/stack/backend/server.js:2663
throw new Error("Module build failed: Error: Typescript emitted no output for /Users/dc/dev/tix/recobot/stack/shared/typezoo/index.d.ts.\n    at successLoader (/Users/dc/dev/tix/recobot/stack/backend/node_modules/ts-loader/dist/index.js:47:15)\n    at Object.loader (/Users/dc/dev/tix/recobot/stack/backend/node_modules/ts-loader/dist/index.js:29:12)");
^

Error: Module build failed: Error: Typescript emitted no output for /Users/dc/dev/tix/recobot/stack/shared/typezoo/index.d.ts.
    at successLoader (/Users/dc/dev/tix/recobot/stack/backend/node_modules/ts-loader/dist/index.js:47:15)
    at Object.loader (/Users/dc/dev/tix/recobot/stack/backend/node_modules/ts-loader/dist/index.js:29:12)
    at Object.__awaiter (/Users/dc/dev/tix/recobot/stack/backend/server.js:2663:7)
    at __webpack_require__ (/Users/dc/dev/tix/recobot/stack/backend/server.js:20:30)
    at Object.defineProperty.value (/Users/dc/dev/tix/recobot/stack/backend/server.js:2551:19)
    at __webpack_require__ (/Users/dc/dev/tix/recobot/stack/backend/server.js:20:30)
    at Object.__awaiter (/Users/dc/dev/tix/recobot/stack/backend/server.js:867:16)
    at __webpack_require__ (/Users/dc/dev/tix/recobot/stack/backend/server.js:20:30)
    at Object.<anonymous> (/Users/dc/dev/tix/recobot/stack/backend/server.js:1306:18)
    at Object.<anonymous> (/Users/dc/dev/tix/recobot/stack/backend/server.js:1343:30)
    at __webpack_require__ (/Users/dc/dev/tix/recobot/stack/backend/server.js:20:30)
    at Object.__awaiter (/Users/dc/dev/tix/recobot/stack/backend/server.js:1028:15)
    at __webpack_require__ (/Users/dc/dev/tix/recobot/stack/backend/server.js:20:30)
    at Object.defineProperty.value (/Users/dc/dev/tix/recobot/stack/backend/server.js:63:18)
    at Object.<anonymous> (/Users/dc/dev/tix/recobot/stack/backend/server.js:66:10)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
[nodemon] app crashed - waiting for file changes before starting...

基于create-react-app,webpack的配置非常原始。

const fs = require("fs")
// const path = require("path")
const NodemonPlugin = require("nodemon-webpack-plugin");

const nodeModules = {};
fs.readdirSync("node_modules")
  .filter(function (x) {
    return [".bin"].indexOf(x) === -1;
  })
  .forEach(function (mod) {
    nodeModules[mod] = "commonjs " + mod;
  });

module.exports = {
  entry: "./server/server.ts",
  output: {
    filename: "server.js",
    // path: path.join(__dirname, "/build"),
  },

  externals: nodeModules,

  // needed to fix https://github.com/webpack/webpack/issues/1599
  node: {
    __dirname: true
  },

  module: {
    loaders: [
      {
        loader: "ts-loader",
        test: /\.tsx?$/,
      },
    ],
  },
  plugins: [new NodemonPlugin()],
  resolve: {
    extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
  },
  target: "node",
};

1 个答案:

答案 0 :(得分:0)

对于正在观看的任何人,该错误都与导入enum s有关。

您可以从enum文件中导出types.d.ts ...但是它不会以这种奇怪的方式导入并爆炸。

https://lukasbehal.com/2017-05-22-enums-in-declaration-files/

How to refer to Typescript enum in d.ts file, when using AMD?