4

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"
}
10
  • 3
    That has to be a bug Commented Aug 12, 2019 at 14:05
  • I'm not immediately seeing a report in the issues list, probably worth reporting there. Commented Aug 12, 2019 at 14:09
  • @T.J.Crowder Same as github.com/microsoft/TypeScript/issues/29263? Commented Aug 12, 2019 at 14:16
  • Are you sure it's TS doing the compilation and not babel ? I can't reproduce this issue in an empty project with just the tsconfig and files you provided Commented Aug 12, 2019 at 14:17
  • 1
    @TitianCernicova-Dragomir I disabled ttsc and compiled with tsc and that fixed the issue. I think it's a bug in ttsc and typescript-transformer-append-js-extension. Commented Aug 12, 2019 at 14:45

1 Answer 1

1

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
    }
],
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.