Aaron Digulla
Aaron Digulla

Reputation: 328624

TypeScript lists imported interface and types in generated JavaScript

I have a module A which exports classes, types and interfaces:

export class Event {
    // empty
}

export interface EventHandler {
    handle(event: Event): void
}

export type EventCallback = (event: Event) => void

I can use this in another module B:

import {
    EventCallback,
    EventHandler,
    Event
} from "./ts_interface_bug"

let x: EventCallback = (event) => {
    console.log(event)
}

class MyHandler implements EventHandler {
    handle(event: Event): void {
    console.log(event)
    }
}

but the compiler keeps the types when generating JavaScript (B.js):

import { EventCallback, EventHandler, Event } from "./ts_interface_bug.js";
let x = (event) => {
    console.log(event);
};
class MyHandler {
    handle(event) {
        console.log(event);
    }
}

This is wrong - no code is generated for types and interfaces as you can see in A.js:

export class Event {
}

Is this a bug or can I somehow configure the TypeScript compiler to omit types and interfaces?

TypeScript 3.5.3

tsconfig-build.json:

"compilerOptions": {
    "allowJs": true,
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true,
    "strict": true,
    "strictPropertyInitialization": false,
    "target": "ES2016",
    "lib": [
        "dom", "ES2016"
    ],
    "module": "ES2015",
    "moduleResolution": "node",
    "typeRoots": [
        "./node_modules/@types"
    ],
    "plugins": [
        {
            "transform": "@zoltu/typescript-transformer-append-js-extension/output/index.js"
        }
    ],
},

package.json:

"scripts": {
    "build": "ttsc --build tsconfig-build.json",
},
"devDependencies": {
  "@zoltu/typescript-transformer-append-js-extension": "^1.0.1",
  "ttypescript": "^1.5.7",
  "typescript": "^3.5.3"
}

Upvotes: 4

Views: 87

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328624

The example above works when using tsc --build tsconfig.json. There must be something wrong either in ttypescript or the typescript-transformer-append-js-extension plugin (which works in import statements to fix https://github.com/microsoft/TypeScript/issues/16577).

I've opened a bug: https://github.com/Zoltu/typescript-transformer-append-js-extension/issues/3

UPDATE Apparently, the bug was that I need to run the transformer last. This fixes it:

"plugins": [
    {
        "transform": "@zoltu/typescript-transformer-append-js-extension/output/index.js",
        "after": true
    }
],

Upvotes: 1

Related Questions