tsconfig.json 中 compilerOptions 配置项 "outDir": "./" "rootDir": "./" "baseUrl": "./" "paths" 请用代码实际的代码演示如何使用
时间: 2023-08-10 08:03:06 浏览: 62
当你的 tsconfig.json 文件中的 compilerOptions 配置项如下所示:
```json{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"baseUrl": "./",
"paths": {
"@utils/*": ["./src/utils/*"]
}
}
}
```
你可以使用以下示例代码来演示如何使用这些配置项:
```typescript// src/utils/math.tsexport function add(a: number, b: number): number {
return a + b;
}
// src/index.tsimport { add } from '@utils/math';
console.log(add(2,3)); // 输出:5```
在上面的示例中,我们在 `src/utils` 目录下创建了一个 `math.ts` 文件,其中导出了一个 `add` 函数。然后在 `src/index.ts` 文件中,我们通过 `import`语句将 `add` 函数导入,并使用它进行加法运算并打印结果。
当我们运行 TypeScript 编译器时,它会根据 tsconfig.json 中的配置将 TypeScript 文件编译为 JavaScript 文件,并输出到指定的目录。
例如,假设我们在命令行中执行 `tsc` 命令来编译 TypeScript 文件,根据上述的配置,编译后的 JavaScript 文件将输出到 `dist` 目录。那么执行 `node dist/index.js` 将会输出 `5`。
希望以上示例能够帮助你理解如何使用 tsconfig.json 中的配置项。如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)