When I Transpile my TypeScript file, the variables declared in 'let' and 'const' are converted to 'var' in generated Javascript file

huangapple go评论72阅读模式
英文:

When I Transpile my TypeScript file, the variables declared in 'let' and 'const' are converted to 'var' in generated Javascript file

问题

I have even made manipulation in tsconfig.json (i.e. target) file.

我甚至在tsconfig.json(即目标)文件中进行了修改。

I have set my "target":"ES6" in tsconfig.json as of now.

我现在在tsconfig.json中设置了"target":"ES6"

While I have tried various solutions, yet the transpilation is not working out correctly. The transpilation works out correctly when I just type tsc command in the terminal.

虽然我尝试了各种解决方案,但转译结果仍然不正确。只要我在终端中键入tsc命令,转译就会正确执行。

This is the code from 'data.ts'

这是来自'data.ts'的代码

const FirstName: string = "John"
let age: number = 23

and the generated 'data.js'

以及生成的'data.js'

var FirstName = "John";
var age = 23;

This is my file structure

这是我的文件结构

英文:

I have even made manipulation in tsconfig.json (i.e. target) file.

I have set my "target":"ES6" in tsconfig.json as of now.

While I have tried various solutions, yet the transpilation is not working out correctly. The transpilation works out correctly when I just type tsc command in the terminal.

This is the code from 'data.ts'

const FirstName: string = "John"
let age: number = 23

and the generated 'data.js'

var FirstName = "John";
var age = 23;

This is my file structure

When I Transpile my TypeScript file, the variables declared in 'let' and 'const' are converted to 'var' in generated Javascript file

答案1

得分: 2

tsc CLI 文档 明确指出了这一点 — 当您在 tsc 命令中包含文件参数时,将忽略您的 TSConfig 配置:

使用 CLI

在本地运行 tsc 将编译由 tsconfig.json 定义的最接近的项目,或者您可以通过传递一组您想要的 TypeScript 文件的 glob 来编译一组文件。当在命令行中指定输入文件时,将忽略 tsconfig.json 文件。

# 通过向后查找文件系统来运行基于 tsconfig.json 的编译
tsc

# 使用编译器默认设置仅发出 index.ts 的 JS
tsc index.ts

如果您想要在编译中包含特定文件,可以在项目配置中使用 includes 字段。

英文:

The tsc CLI documentation makes this clear — your TSConfig is ignored when you include file arguments to tsc:

> ## Using the CLI
>
> Running tsc locally will compile the closest project defined by a tsconfig.json, or you can compile a set of TypeScript files by passing in a glob of files you want. When input files are specified on the command line, tsconfig.json files are ignored.
>
> lang-sh
> # Run a compile based on a backwards look through the fs for a tsconfig.json
> tsc
>
> # Emit JS for just the index.ts with the compiler defaults
> tsc index.ts
>

If you want to include certain files in the compilation, you can use the includes field in your project config.

huangapple
  • 本文由 发表于 2023年7月24日 15:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定