I worte some code to test typescript how to translate ts -> js.

const printX = (name: string) => console.log(`hello, ${name}!`)
const tt = () => {
 const t1 = 123;
const t2 = 456;

}

I checked the translated js file and it was,

var printX = function (name) { return console.log("hello, ".concat(name, "!")); };
var tt = function () {
    var t1 = 123;
    var t2 = 456;
};

Why Typescript translate from const keyword to var keyword.

I'm wondering if I can make ts translate force const to const.

推荐答案

Set the target in your tsconfig to something above ES5, and then ES6 syntax (such as const) will be preserved.

Modern browsers support all ES6 features, so ES6 is a good choice. You might choose to set a lower target if your code is deployed to older environments, or a higher target if your code is guaranteed to run in newer environments.

The target setting changes which JS features are downleveled and which are left intact. For example, an arrow function () => this will be turned into an equivalent function expression if target is ES5 or lower.

Typescript相关问答推荐

如果请求是流,如何等待所有响应块(Axios)

类型脚本`Return;`vs`Return unfined;`

TS如何不立即将表达式转换为完全不可变?

TypeScrip省略了类型参数,仅当传递另一个前面的类型参数时才采用默认类型

是否可以针对联合类型验证类型属性名称?

获取一个TypeScrip对象,并将每个属性转换为独立对象的联合

TypeError:正文不可用-NextJS服务器操作POST

是否可以判断某个类型是否已合并为内部类型?

Typescript 类型守卫一个类泛型?

使用来自API调用的JSON数据的Angular

无法缩小深度嵌套对象props 的范围

如何避免多个类型参数重复?

替换typescript错误;X类型的表达式可以';t用于索引类型Y;带有未定义

如何填写Partial的一些属性并让类型系统知道它?

如何使用动态生成的键和值定义对象的形状?

为什么 typescript 在对象合并期间无法判断无效键?

组件的第二个实例使用 svelte 中第一个实例上设置的函数

如果在另一个对象中指定了递归省略字段?

如何在Typescript 中使用 SelectMany

ChartComponent类型中缺少属性type,但ApexChart类型中需要属性type