Typescript generates legacy JavaScript code although target is "es2018"

41 Views Asked by At

while trying to learn Typescript basics, I ran into this weird problem:

I am targeting "es2018" but the generated JS code is older...I think it is "ES5"

what am I doing wrong here?

index.ts

import { sum } from "./calc";

function printMessage(msg: string): void {
  console.log(`Message: ${msg}`);
}
printMessage("Hello, TypeScript developer Raki Lachraf");
printMessage("It is sunny today");
let data = new Map();
data.set("Bob", "London");
data.set("Alice", "Paris");
data.forEach((val, key) => console.log(`${key} lives in ${val}`));

let total = sum(100, 200, 300);
console.log(`Total: ${total}`);

calc.ts

export function sum(...vals: number[]): number {
  return vals.reduce((total, val) => (total += val));
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2018",
    "outDir": "./dist",
    "rootDir": "./src",
    "noEmitOnError": true,
    "module": "CommonJS",
    "lib": ["ES2018", "DOM"]
  }
}

index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const calc_1 = require("./calc");
function printMessage(msg) {
    console.log(`Message: ${msg}`);
}
printMessage("Hello, TypeScript developer Raki Lachraf");
printMessage("It is sunny today");
let data = new Map();
data.set("Bob", "London");
data.set("Alice", "Paris");
data.forEach((val, key) => console.log(`${key} lives in ${val}`));
let total = calc_1.sum(100, 200, 300);
console.log(`Total: ${total}`);

package.json

{
  "name": "tools",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "tsc-watch --onsuccess \"node dist/index.js\"",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "tsc-watch": "^4.2.9",
    "typescript": "^4.2.2"
  }
}

I tried to declare the "lib" compiler option as "es2018" and "DOM" but no good

0

There are 0 best solutions below