transpile TypeScript code into ES6 rather than ES5

I am currently diving into typescript and I have observed that the code output always defaults to using var. Is there a way to make it output const and let in the .js file instead, or does typescript always require es5 output for some reason?
Thanks.

Here is an example:

// main.ts
const x: number = 2;
let y: string = 'hello';

// main.js
var x = 2;
var y = 'hello';

If this kind of output is possible, how exactly can one achieve it?

// main.js
const x = 2;
let y = 'hello';

Answer №1

If you want to set a specific target for compilation, simply include compiler flags like --target es6. For more information, check out this related question: How do I transpile TypeScript to ES6?

Answer №2

After including tsconfig.js and configuring it as follows:

{
  "compilerOptions": {
    "target": "es2015"
  }
}

I was still encountering issues.

However, when I executed npx tsc instead of npx tsc xxx.ts, I was able to resolve the problem and received 'const'.

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

Is it possible to eliminate process.env.NODE_ENV using browserify/envify?

Currently, I am utilizing ReactJS through NPM and Browserify, but I am encountering difficulties while attempting to build it in production mode as mentioned in the readme. The code I have for setting up browserify is: var browserify = require('brows ...

What is the most efficient way to extract the observable value property from an observable array?

I am working on a function called getLocationForEquipment, which is meant to take an array of observable locations, filter them based on a locationid parameter, and then return the location name for that specific location. I need to figure out how to utili ...

Issue with binding the expanded property in Angular Expansion Panel

Including Angular code within my HTML, I have Admin and User Main menu items available. <a id="adminId" (click)="sideNavLink($event)">Admin</a> <mat-accordion class="mat-accordion-setting"> <mat-expansi ...

Placing an iframe at the end of a container

I am attempting to use jQuery to insert an iframe into a div. The current code accomplishes this, but it also erases all existing content within the div#off. How can I make the iframe appear at the bottom of the content in the div#off without removing any ...

What is the best way to utilize Gulp and Browserify for my JavaScript application?

Looking to modernize my app with a new build system and I could use some guidance. It seems like I need to shift my approach in a few ways. Here's the current structure of my app: /src /components /Base /App.jsx /Pages.jsx /. ...

Eavesdrop on outbound requests on a web page using JavaScript

As I develop a single-page application that integrates a third-party analytics tool (such as Heap, Segment, Mixpanel, etc.), I am looking to actively monitor the outgoing HTTPS requests from my app and take necessary actions, such as logging. I am curious ...

Filtering a nested array based on the value of an element's field in

I am trying to filter a nested array based on the value of an element field. The array that needs to be filtered is shown below. I want to filter by the checked value of each element. If the value is true, I want to keep the element; if false, discard it. ...

Showing various information in tab form depending on a specified variable

In my current project, I am developing a user-friendly view that allows users to easily switch between different sets of data. The specific scenario involves a manager overseeing multiple stores with various franchises. Our dashboard presents aggregated re ...

Utilize JavaScript to compel image caching

I've run into an issue where I am trying to duplicate a randomly generated image, but despite using the same URL, a different image is being loaded every time. This problem has been confirmed in both Chrome and Firefox browsers. Unfortunately, since ...

Setting a JavaScript variable to null

Using Jquery, I have a variable that is assigned a value on button click. After the code executes successfully, I need to reset the variable to null. $("#btnAdd").click(function () { var myDataVariable= {}; myDataVariable.dataOne="SomeDa ...

Error: JavaScript alert not displaying

My asp.net page is not displaying the Javascript alert message until I clear the browser's cache memory. Can someone please explain why this happens and suggest a solution? ...

Struggling to retrieve information from session storage to pass along to a PHP class

Is there a way to fetch the email of the currently logged-in user from sessionStorage and send it to a PHP file? I have tried implementing this, but it seems to be not functioning properly. Could you assist me in resolving this issue? <?php $mongoCl ...

Reduce the number of redundant fields in MongoDB collections

I'm facing an issue with my model structure. Here is the schema: var accountSchema = new mongoose.Schema({ 'seeker': { 'fullName': String, 'ageGroup': String, 'education': String, ...

The code for the bouncing image isn't functioning properly outside of the JSFiddle environment

I'm having issues with this jQuery snippet in my web application. It works fine on jsfiddle, but not when I add it to my project. Here's the code: $('.myimage').mouseenter(function() { $(this).effect('bounce',500); }); Her ...

Working with nested JSON data in Node.js independently

Here is an example of a JSON structure: { "username": { "A1": { "L1": "usernameL1", "V1": "usernameV1" }, "A2": { "L2": "usernameL2", "V2": "usernameV2" } }, "pass ...

Struggling with updating scope values when binding data in Angular (particularly with a slider)

Currently, I am utilizing Angular to develop a tool that can take user input from a slider tool and dynamically update an "estimate" field whenever the values are adjusted. However, I'm encountering an issue where the data is only binding in one direc ...

Separate the divs in a template into individual templates, or alternatively, utilize ng-if to display different divs within a single template

As a newcomer to AngularJS, I am seeking advice on the most efficient way to code for the given scenario: Scenario: I have a single HTML template containing multiple divs, and I need to display only one div at a time based on specific conditions. Two app ...

Troubleshooting issue with Angular's inability to loop through a multi-dimensional JSON

I recently started experimenting with Angular and decided to create a shopping cart application. I found a pre-made site template that organizes items into categories using the following structure: <div class="row"> <ul> <li>item1</li ...

Issue with TypeScript: "Cannot find name" error encountered during yarn build

I'm encountering an issue with my .tsx component code below: const Header = ({ dict: map, lang: string }) => { return ( <header className="flex items-center justify-between py-10"> <div> <Link href={`/${ ...

How to trigger a click event on a div and effectively filter Vuex data in Vue.js?

Currently facing a challenge with exporting the filter function and utilizing it in a vuex store. Everything was smooth sailing until this point. Now, I'm attempting to add an @click event to divs. When I click on a particular brand, such as "Audi," I ...