导出像" module.exports"在TS

时间:2018-04-28 17:08:57

标签: typescript yeoman

我正在写一个Yeoman生成器,当我在JS中这样做时:

"use strict";
var Generator = require("yeoman-generator");
var chalk = rquire("chalk");
module.exports = class extends Generator {
  initializing() {
    this.log(
      chalk.bold(
        "Welcome to the " + chalk.green("TypeScript for Serverless") + " generator!"
      )
    );
};
一切都很好。但我想我可以在TypeScript中这样做:

import Base = require("yeoman-generator");
import chalk from "chalk";
type IFileConfiguration = IComplexFileConfiguration | string;

export class Generator extends Base {
  public options: IDictionary;
  public initializing() {
    this.log(
      chalk.bold(
        "Welcome to the " + chalk.green("TypeScript for Serverless") + " generator!"
      )
    );
 }

我的tsconfig.json是:

{
  "compilerOptions": {
    "declaration": true,
    "module": "commonjs",
    "lib": ["es2015", "es2016", "es2017", "esnext.asynciterable"],
    "target": "es2015",
    "moduleResolution": "node",
    "sourceMap": false,
    "noImplicitAny": true,
    "outDir": "./generators/app",
    "removeComments": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*-spec.ts"]
}

但是这会创建一个不同的模块定义,并且会完全打破Yeoman接口的期望。有没有办法让我将已编译的JS转换为与上面列出的相同的导出格式?

1 个答案:

答案 0 :(得分:1)

好的我明白了。通过定义没有导出运算符的类:

SET TIME_ZONE='+00:00'

然后我可以在TS文件的末尾导出,如下所示:

class Generator extends Base {}

问题解决了。