Avatar ·

如何为TypeScript项目创建tsconfig.json:分步指南

大家好!我是TypeScript新手,一直搞不懂项目配置。我有一个`app.ts`文件,想编译成JavaScript,但TypeScript对我的任何尝试都报错。 我是这样做的: 1. 创建项目文件夹。 2. 全局安装TypeScript:`npm install -g typescript`。 3. 尝试用命令`tsc app.ts`编译文件。 但编译器报错,比如`Cannot find name 'console'`或`Cannot compile modules unless the '--module' flag is provided`。我听说需要创建`tsconfig.json`,但不知道怎么做。谷歌说可以运行`tsc --init`,但我不确定这是否正确。 我的代码: ```typescript // app.ts function greet(name: string): string { return `Hello, ${name}!`; } const user = "Alice"; console.log(greet(user)); ``` 请告诉我如何正确生成`tsconfig.json`,以及需要添加哪些基本设置才能让项目运行?是否需要本地安装TypeScript?