Is it necessary to have duplicate variables in the JavaScript file when using Typescript?

Just starting out with typescript. Here's the code I've been experimenting with:

module app.controllers {
  export class FooController {

  }
}

module app.controllers {
  export class BarController {

  }
}

After running tsc with options --module amd and outFile app.js, this is the generated result:

var app;
(function (app) {
    var controllers;
    (function (controllers) {
        var FooController = (function () {
            function FooController() {
            }
            return FooController;
        }());
        controllers.FooController = FooController;
    })(controllers = app.controllers || (app.controllers = {}));
})(app || (app = {}));
var app;
(function (app) {
    var controllers;
    (function (controllers) {
        var BarController = (function () {
            function BarController() {
            }
            return BarController;
        }());
        controllers.BarController = BarController;
    })(controllers = app.controllers || (app.controllers = {}));
})(app || (app = {}));

I noticed that there are 2 instances of var app; being generated. It seems like it should only be generated once. Is there a way to achieve a cleaner structure like this?

var app;
(function (app) {
    var controllers;
    (function (controllers) {
        var FooController = (function () {
            function FooController() {
            }
            return FooController;
        }());
        controllers.FooController = FooController;

        var BarController = (function () {
            function BarController() {
            }
            return BarController;
        }());
        controllers.BarController = BarController;
    })(controllers = app.controllers || (app.controllers = {}));
})(app || (app = {}));

Is this optimization possible? If not, are there any tools available to simplify the generated javascript code to achieve the desired output? Thanks in advance!

Answer №1

Nah, there's no need for it.

Due to variable hoisting, only one variable called app will exist at runtime.

To learn more about hoisting, check out this link.

Regarding the separate compilation of modules, that's something you can't alter either.

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

Guide to creating a SVG component using typescript in a React application

I have a component where I am passing SVG icons props as an array containing my SVG component data. However, TypeScript is showing me an error while working with Material UI. Type '{ key: number; data: { id: number; icon: ReactNode; }; }' is not ...

Guide to creating content on an NFC tag with Ionic

I am struggling with my button calling the test2 function and the code I have is not working as expected. Here is what I currently have: import { Component } from '@angular/core'; import { NFC, Ndef } from '@ionic-native/nfc/ngx'; @Com ...

The FormGroup Matrix in Angular 2+

Looking to develop an interface for creating a matrix, which requires two inputs for width and height. The matrix of inputs will vary based on these dimensions. I'm facing a challenge in making these inputs unique so they can be correctly associated ...

Unable to determine all parameters for Modal: (?, ?, ?)

import { Component, Inject } from '@angular/core'; import { NavController, Modal } from 'ionic-angular'; import { PopupPage } from '../../components/modal/modal.page'; @Component({ templateUrl: 'build/pages/spot/spot. ...

"Error: File is not recognized - Issue encountered while attempting to instantiate a File Object using types

I am working on a TypeScript program in Node.js, specifically a console application and not an API or web app. I have set up the TypeScript configuration, but I encountered an error when trying to create a File Object. UnhandledPromiseRejectionWarning: Ref ...

Error: The route in "src/app/api/orders/route.ts" does not conform to the necessary types for a Next.js Route. The "default" export field is not recognized as a valid Route

I need to retrieve the orders from the database using Prisma based on the user's email (authenticated via Google Provider). Here is the repository - https://github.com/Jayesh-kahnani/Snatch Here is the API. // src/app/api/order/route.ts import { Next ...

Having trouble displaying the button upon initial load using ngIf

My goal is to display a button when editing an input form. Initially, the button is hidden when the page loads but it should appear once any of the input fields are edited. I have also implemented highlighting for the input box that is being edited. Howeve ...

If the value is null, pass it as is; if it is not null, convert it to a date using the

I am currently facing an issue regarding passing a date value into the rrule plugin of a fullCalendar. Here is the snippet of code in question: Endate = null; rrule: { freq: "Daily", interval: 1, dtstart: StartDate.toDate ...

Most effective method for integrating a JS module across all browsers

I have created a robust library that I want to integrate into a different web project. This library handles all its dependencies and consists of js, css, and html files. My ideal scenario would be to package it as an npm module and simply use import to in ...

Using Angular: connecting $viewContentLoaded to manually initiate app bootstrapping

I have an Angular module that I am bootstrapping manually and attempting to access its $rootScope to add an event listener for $viewContentLoaded. Below is the code snippet: angular.bootstrap(el, [appname]); //////////////////////////// Fixing links cons ...

Encountering an issue with a Node native module not being found while attempting to import

Encountering an issue while working on a Svelte & TypeScript project where importing native Node modules is causing trouble. For instance, when typing: import { createInterface } from "readline"; in a .ts or .svelte file, a warning pops up saying: Plugin ...

I noticed that the ./src/main.js file is present in multi (webpack)-dev-server/client, but unfortunately I do not have the main.js file. I am

I'm currently working on a Vue.js 3 application demo using tailwind and typescript. Each time I try to run the app, I encounter an error message that says: This relative module was not found: * ./src/main.js in multi (webpack)-dev-server/client?https ...

TypeScript class-module declaration file

I am encountering an issue with a node module that is not recognized by typings and is not available in definelytyped. The basic usage of this module is shown below: import * as SomeClass from 'some-module'; var someObject = new SomeClass("som ...

What is the best method to find a matching property in one array from another?

I am working with two arrays in TypeScript. The first one is a products array containing objects with product names and IDs, like this: const products = [ { product: 'prod_aaa', name: 'Starter' }, { product: 'prod_bbb&apos ...

Is it possible to assign default values to optional properties in JavaScript?

Here is an example to consider: interface Parameters { label: string; quantity?: number; } const defaultSettings = { label: 'Example', quantity: 10, }; function setup({ label, quantity }: Parameters = { ...defaultSettings }) { ...

Unable to display information on mat table without receiving an error message

Hello everyone, I am a novice in Angular and I seem to be encountering an issue with displaying data on my mat table. Surprisingly, no errors are being shown. While I can see the column names, the actual data is not visible. Can someone help me figure ou ...

Tips for creating a custom script in my React Native application

My React Native app requires a script to generate static files during the release process. The app is a game that utilizes pre-computed boards, which are resource-intensive to compute. Therefore, I am developing a script that will create these boards and s ...

Use MatDialog to open the next dialog when the previous one has been closed - whether you choose to use a

I'm trying to figure out how to open the next dialog from a stream only after the previous one has been closed. I want to make sure that I don't open all the dialogs in a row as soon as I get the values from the stream. const arraySource ...

Obtain PDF File using Typescript

I am attempting to use an AJAX Post to download a PDF file and return the templateFile Model. However, I am encountering an error where it cannot convert type TemplateFileDto to IhttpActionResult. Should I consider returning something different? Any assist ...

the ad grid fails to load properly on the page

In the midst of developing my angular 4 project, which is based on a starter template found here, I encountered an issue while trying to integrate ag-grid into my project. Following the instructions provided here, I successfully created a new example proj ...