Visual Studio Code 中 TypeScript 文件中的绝对模块路径解析

2024-02-08

我似乎无法说服 Visual Studio Code 解析绝对 TypeScript 模块路径。相对路径有效,但绝对路径无效。我希望 Visual Studio Code 能够解析来自./src文件夹上。

// This works when source file is in /src/here/there/file.ts
// and importing an interface in /src/api/interfaces.ts
import { Interface } from '../../api/interfaces';

// This doesn't work
import { Interface } from 'api/interfaces';
import { Interface } from '/api/interfaces';
import { Interface } from 'src/api/interfaces';
import { Interface } from '/src/api/interfaces';

// This works, but it is of course not supposed to be used
import { Interface } from 'c:/..../src/api/interfaces';

最后一个当然不算数,因为每个开发人员的项目路径很可能不同。但即使我们都设置了一个系统变量%ProjectXRoot%我们不能在代码中使用这个变量。 Visual Studio Code 不会解析此类模块路径。我试过了。

// Won't work
import { Interface } from '%ProjectXRoot%/api/interfaces';

当前安装的版本
•打字稿:1.8.10
VSCode:1.1.1

Question

我尝试以某种方式配置 Visual Studio Code 来解析绝对模块路径,但我似乎无法这样做。我尝试过配置tsconfig.json(在项目根目录中)添加baseUrl在两个不同的地方。

{
    ...
    "compilerOptions": {
        "baseUrl": "./src", // Doesn't work
        ...
    },
    "baseUrl": "./src", // Doesn't work either
    ...
}

我尝试过类似的值src, ./src and ./src/,但它们都不能在任何较高配置的地方工作。

So 如何配置 Visual Studio Code解析某个文件夹中的绝对模块路径?

如果这是不可能的,那么至少从项目根目录解析绝对路径会更好。 Visual Studio Code 如何确定这一点?是你的地方吗打开文件夹或者是在哪里.vscode文件夹是?

但它仍然是绝对路径的可行解决方案。我尝试过使用~与 Visual Studio 类似,但也无济于事。

(Unresolved)

-- As steinso指出在他的回答 https://stackoverflow.com/questions/37606571/absolute-module-path-resolution-in-typescript-files-in-vscode/37655471#37655471,所有模块以/, ./ or ../被认为是相对的。尤其是第一个完全让我惊讶,因为我通常认为这是一个项目根相对路径。

But this fact basically means that the main question now becomes: How can I provide module imports as absolute paths (from some project root folder path) at all? Starting paths with slashes usually meant absolute, but in this case it doesn't.

即使我设置了编译器选项moduleResolution to classic(所以模块解析不会考虑node_modules文件夹)上面的第二组导入实际上应该all work根据微软的链接文档。但由于某种原因,我仍然在 Visual Studio Code 中看到红色波浪线,并且在编译过程中出现错误。

那么我怎样才能导入特定的项目模块而不提供确切的相对路径到它,而只是它自己的项目相对路径?


为了能够使用 Visual Studio Code 在 TypeScript 中导入时使用绝对路径,您应该使用下一版本的 TypeScript -typescript@next这是 TypeScript v2。为此,请执行以下操作:

  1. Install 打字稿@下一个通过 npm。用于安装 TypeScript v2.x

    npm i typescript@next -D

  2. 在 Visual Studio 代码中

    i) 转到菜单File优先工作区设置(这会生成.vscode项目根目录下并初始化settings.json file.)

    ii) 输入以下内容key:value配对settings.json file

    "typescript.tsdk": "node_modules/typescript/lib"

  3. In tsconfig.json添加以下键:值对'compilerOptions'

{
  "compilerOptions" : {
    "baseUrl": "./",
    "paths" : {
      "src/*": ["./src/*"]
    }
  }
}
  1. 重新加载 Visual Studio 代码

如果您有以下目录结构:

+ node_modules
+ src
| + app
| |  + shared
| |  |  -service.ts
| |  -main.ts
+ typings
- tsconfig.json
- webpack.config.json
- package.json
- index.html

然后导入/src/app/shared/service.ts from main.ts你现在可以import {} from 'src/app/shared/service;

如果您正在使用webpack and ts-loader用于转译.ts文件,您应该将以下内容添加到resolve的部分webpack.config.js配置文件。

resolve: {
    extensions: ['', '.js', '.ts'],
    alias: {
      "src": path.resolve('./src')
    }
  }

请参阅this https://github.com/Microsoft/TypeScript/issues/5039绝对模块分辨率。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Visual Studio Code 中 TypeScript 文件中的绝对模块路径解析 的相关文章

随机推荐