Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting like "module.exports" in TS

I am writing a Yeoman generator and when I do it in JS like so:

"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!"
      )
    );
};

all is good. But I figured I could do this in TypeScript like so:

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!"
      )
    );
 }

My tsconfig.json is:

{
  "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"]
}

But this creates a different module definition and one that completely breaks the Yeoman interface expectation. Is there a way for me to get the transpiled JS into the same export format as listed above?

like image 944
ken Avatar asked Oct 22 '25 05:10

ken


1 Answers

Ok I figured it out. By defining the class without the export operator:

class Generator extends Base {}

I can then export at the end of the TS file like so:

export = Generator

Problem solved.

like image 112
ken Avatar answered Oct 23 '25 21:10

ken