In the generated JavaScript code, TypeScript displays imported interfaces and types in a list format

In my module A, I have a set of classes, types, and interfaces being exported:

export class Event {
    // empty
}

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

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

These can be utilized in another module B as shown below:

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)
    }
}

However, when compiling to JavaScript (B.js), the compiler retains the types:

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

This behavior is incorrect - no code for types and interfaces should be generated like in A.js:

export class Event {
}

Is this a bug, or is there a way to configure the TypeScript compiler to exclude 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"
}

Answer №1

When running tsc --build tsconfig.json, the example above functions properly. It seems like there may be an issue with either ttypescript or the

typescript-transformer-append-js-extension
plugin (which is used in import statements to resolve https://github.com/microsoft/TypeScript/issues/16577).

A bug report has been submitted here: https://github.com/Zoltu/typescript-transformer-append-js-extension/issues/3

UPDATE It appears that the bug stemmed from not running the transformer as the last step. Following this configuration fixes the issue:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a reusable function in AngularJS using the factory method

Currently, I am using the AngularJS factory method to retrieve data and passing that value to controllers. In order to avoid creating separate functions, I would like to create a common function that can be utilized in all controllers. How can I effi ...

Tips for Iterating through Nested Arrays using the Inside Array in a Dynamic Way

I am facing an issue with my code as it lacks flexibility when a new array is added to the nested array, in which case the new array is not considered. My main concern is how to access the elements of each nested array simultaneously. Here's an examp ...

Implement a dropdown menu for filtering, but it is currently not functioning as expected

When I select a city_name, my goal is for the graph to only display information pertaining to that particular city. In the params section of my code, I have included filtering options using a selection menu in Vega-Lite. However, despite selecting Brisba ...

Creating a dynamic view that incorporates all the necessary data through the use of Jade, Express, and

In my mongodb setup, I have two collections - notes, which contains individual notes, and notebook, which holds titles along with an array of notes that are referenced by their ids from the notes collection. //example data in notes: { "_id" : ...

Exploring the source code of NPM public and private packages within the node_modules directory

As someone who is new to javascript development, I want to create a private npm package that cannot be accessed by users. However, I have noticed that I can still view the code of other npm packages labeled as closed-source by entering their node_modules s ...

Encountering an issue post-upgrade with Angular 7 project

Currently, I am facing an issue with upgrading a project from Angular 6 to version 7. Despite following multiple online tutorials and successfully completing the upgrade process, I encountered an error when running the 'ng serve' command: ERROR ...

Ways to transfer selected options from a dropdown menu to a higher-level component

I am currently in the process of configuring a modal component that showcases various data from a specific record to the user. The user is provided with a Bulma dropdown component for each field, enabling them to make changes as needed. To streamline the c ...

In order to display() or delete() a specific element based on the logged-in user, it is necessary

(function($){ $(document).ready(function() { $('.nlgchat').hide(); // hiding the chat section by default var usernames = ['Mr.EasyBB','runawayhorses','BL@DE','SirChivas']; var nameslist='&ap ...

"Combine elements into groups and calculate their sum using the 'groupBy

Below is an example of an array: let transactions = [{ date: 2019, amount: 3000 }, { date: 2019, amount: 5500 }, { date: 2020, amount: 2300 } ] I am looking to calculate the total amount for each year, resulting in the fo ...

Utilizing the Java Script SDK for Facebook API to Publish Actions

I've been encountering difficulties with the Facebook API. I have created an app, an object, and an action, and I'm trying to test publishing in my stream (I understand that public publishing needs authorization from Facebook, but as an administr ...

Ways to conceal a message button on a website when a user logs out

I am looking for assistance with hiding the message button on my website after a user logs out. The message option should be disabled upon logout using a combination of Javascript and PHP. Can anyone help me with this issue? Below is the code for the butt ...

What could be causing the error stating that objects are not valid as a react child when I use touchable opacity?

Here's the main code snippet from app.js: import React, { useState } from "react"; import { StyleSheet, Text, View, SafeAreaView, TextInput, Platform, TouchableOpacity, } from "react-native"; import tailwind from &quo ...

Unable to adjust dimensions with CSS width and height properties

I'm currently working on developing an online game with a login screen inspired by Paper.io. I have created the username input and play button, but there seems to be an issue with the width and height specified in the CSS file for the input field. Eve ...

Modify CSS using Javascript by targeting the class of ".modalDialog > div {}"

I am currently working on a project that involves creating a Modal Layer using CSS. I have successfully implemented the modal layer, but now I need to figure out how to change the width from the default 400px to something else when I open it with Javascrip ...

Discovering the most concise string within the array

I've been working on a JavaScript program function that is supposed to return the smallest string in an array, but I keep encountering an error whenever I run it. Below is the code I have written: function findShortestWordAmongMixedElements(arr) { ...

Exploring AngularJS: Understanding the Differences Between $http's Success and Then

Can someone explain the difference between these methods for me? I am curious about the distinctions between .then and .success functions, as well as .error. Thank you. // Simple GET request example: $http({ method: 'GET', url: '/some ...

The process of ensuring a component is able to watch for the router even when it is not within the router

I am facing an issue with setting v-if for an element to get a boolean value from a function when the router changes the URL. Here is the code snippet for my Header component: <template> <header class="wfm-header"> <div class=" ...

Obtain the content of a dynamic text input field from a nested directive

I recently developed a custom directive using AngularJS that contains a child directive. The child directive's main task is to create various dynamic input elements like textboxes, radio buttons, and checkboxes, each with default values sourced from a ...

How to disable or enable a submit button in jQuery 1.8

Recently, I upgraded from jquery version 1.5.2 to 1.9 and encountered an issue with my disabled buttons not functioning properly. The buttons should become enabled after the form fields are filled out, allowing the user to either remove or save the infor ...

Issue with Angular application failing to fetch data from JSON server

I've been attempting to retrieve data from a local server, but so far I'm not getting any results. The concept is to have a service handle the retrieval process and return an observable for any components in need of the data to subscribe to. dis ...