标签: tsconfig-paths

不得设置 compilerOptions.paths(不支持别名导入)

我正在尝试映射 tsconfig.json 中的路径以摆脱相对路径地狱。我的React App 基于 Create-React-App。我尝试了这个SO 线程并在我的 tsconfig.json 中添加了路径。我的 tsconfig.json 是

{
  "compilerOptions": {
    "baseUrl": "src",
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "strictNullChecks": false
  },
  "include": [
    "src"
  ]
}

Run Code Online (Sandbox Code Playgroud)

当我在 VS Code 中编译我的项目时,它会从 tsconfig.json 中删除路径条目,并显示以下消息。为什么我基于 React脚本的 React 项目不支持别名导入?

在此处输入图片说明

typescript reactjs tsconfig tsconfig-paths

6
推荐指数
3
解决办法
1799
查看次数

我可以在 VSCode IDE 上设置要使用的打字稿配置“tsconfig.json”路径吗?

我有一个如下的项目结构:

\n
.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my-app/\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .configs/\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 webpack.merge.ts\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 webpack.dev.config.ts\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 webpack.prod.config.ts\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node_modules\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src/\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.tsx\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package.json\n
Run Code Online (Sandbox Code Playgroud)\n

在命令行上运行 TypeScripttsc --project .configs/tsconfig.json编译不会出现问题。但 VSCode IDE 在.tsx. 我不想禁用 TS 警告,因此这对我不起作用:How to disable TypeScript warnings in VSCode? ,当模块存在时,为什么 VS Code 会抛出“找不到模块 'typescript'.ts(2307)”?

\n

VSCode 中是否有一个设置,我设置了一个路径,以便 VSCode IDE TypeScript 可以用于当前项目。我知道如果我将 放在tsconfig.json根目录 ( my-app) 中,VSCode IDE 将按预期工作。但我想将所有配置脚本保存在一个文件夹中而不是根目录中。VSCode 允许这样做吗?

\n

typescript tsconfig visual-studio-code vscode-extensions tsconfig-paths

6
推荐指数
1
解决办法
9782
查看次数

如何为“tsc-watch”注册“tsconfig-paths”?

我正在尝试为运行它的应用程序设置相对路径tsc-watch。但是当我尝试从功能文件夹访问共享文件夹中的枚举时,我收到“错误:找不到模块”。运行应用程序工作正常。我如何使用with ?ts-nodetsconfig-pathstsc-watch

这是我的项目的仓库:https ://github.com/Nako68l/nestjs-task-management

tsc nestjs tsconfig-paths

5
推荐指数
1
解决办法
4860
查看次数

运行时未正确解析打字稿路径别名

我正在运行 VS Code,目前正在尝试在我的打字稿项目上设置一些别名。

我的开发设置依赖于 nodemon 和 ts-node,代码被编译到一个 dist 文件夹。

到目前为止,我成功地让 Typescript Hero 使用别名来管理导入:

别名解析 vscode

到目前为止,我的文件夹结构是:

.
??? src
  ???modules
  ?????Category
  ?????Ressource
  ???shared
  ?????debug

Run Code Online (Sandbox Code Playgroud)
// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "pretty": true,
        "sourceMap": true,
        "target": "es6",
        "outDir": "./dist",
        "baseUrl": "./src",
        "paths": {
            "@shared/*": [
                "shared/*"
            ],
            "@modules/*": [
                "modules/*"
            ]
        },
        "resolveJsonModule": true,
        "esModuleInterop": true
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts",
        "**/*.test.ts",
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是第一个失败的别名导入。

//Server.ts file
import Print from '@shared/debug/Print.class';
import App from './App';

const …
Run Code Online (Sandbox Code Playgroud)

node.js typescript visual-studio-code tsconfig-paths

5
推荐指数
5
解决办法
8401
查看次数

是否可以将环境变量添加到 tsconfig.json 文件中?

我有一个 tsconfig.json 文件包含

{
  "extends": "../../zlux-app-manager/virtual-desktop/plugin-config/tsconfig.base.json",
  "include": [
    "src/**/*.ts",
    "../../zlux-platform/interface/src/mvd-hosting.d.ts"
  ],
  "compilerOptions": {
    "skipLibCheck": true
  }
}
Run Code Online (Sandbox Code Playgroud)

我想用环境变量替换../../ 。可能的解决方案有哪些?

development-environment environment-variables typescript tsconfig tsconfig-paths

5
推荐指数
1
解决办法
8370
查看次数

无服务器框架无法识别 tsconfig 的路径

我正在尝试使用无服务器框架将 Node.js 应用程序部署到 Lambda,但是,我的 Node 代码使用 tsconfig.json 中的路径来引用导入,但无服务器部署过程失败。如何连接 serverless.yml 来确认并使用 tsconfig.json 中定义的路径?

我已经安装了 serverless-plugin-typescript,但它似乎无法识别 tsconfig 中的路径。

无服务器.yml

service:
  name: app-name

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

plugins:
  - serverless-webpack
  - serverless-plugin-typescript

...
Run Code Online (Sandbox Code Playgroud)

tsconfig.ts

{
  "compileOnSave": true,
  "compilerOptions": {
    "lib": [
      "es2017"
    ],
    "baseUrl": "./src",
    "declaration": true,
    "downlevelIteration": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "./dist",
    "paths": {
      "@config/*": [
        "config/*"
      ],
      "@core/*": [
        "core/*"
      ],
      "@infra/*": [
        "infra/*"
      ],
      "@modules/*": [
        "modules/*"
      ], …
Run Code Online (Sandbox Code Playgroud)

node.js aws-lambda serverless-framework tsconfig-paths

5
推荐指数
1
解决办法
3091
查看次数

在 tsconfig 中使用 TypeScript 路径映射进行短导入

我正在尝试为我的 Angular 模块创建简短的导入。

以下是我在 tsconfig.base.json 文件中修改的内容。

{
  "compileOnSave": false,
  "compilerOptions": {
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "baseUrl": "",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom",
      "esnext"
    ],
    "module": "esnext",
    "paths": {
      "@asfc/shared": ["projects/asfc-shared/src"],
      "@asfc/shared/*": ["projects/asfc-shared/*"],
      "@asfc/shared/global": ["projects/asfc-shared/src/lib/global"],
      "@asfc/shared/global/*": ["projects/asfc-shared/src/lib/global/*"],
      "@asfc/shared/tagging": ["projects/asfc-shared/src/lib/tagging"],
      "@asfc/shared/tagging/*": ["projects/asfc-shared/src/lib/tagging/*"],
      "@asfc/shared/testing": ["projects/asfc-shared/src/lib/testing"]
    },
  "exclude": [
    "tools"
  ]
}
Run Code Online (Sandbox Code Playgroud)

模块路径:projects/asfc-shared/src/public_api.ts

export * from './lib/global/index';
export * from './lib/tagging/index';
export * from './lib/testing/index';
export * from …
Run Code Online (Sandbox Code Playgroud)

typescript tsconfig angular tsconfig-paths

5
推荐指数
1
解决办法
5188
查看次数

发布带有路径和 tsconfig-paths 的打字稿库

我想将共享的通用代码放入私有的 npm 注册表中。

使用下面的命令和 tsconfig 一直工作得很好,直到我想通过发布到 npm 注册表使一个项目依赖于另一个项目。

我有几个文件夹和很多文件,所以我使用路径和 tsconfig-paths (导入看起来很干净,例如import { Repo } from "adapters/api/repository";)。

常见项目可以自行正确构建,但是一旦发布并用作依赖项,当我构建使用它的项目时,它就会出现构建错误。

共同项目;构建良好:与rm -rf dist && tsc --sourceMap -p ./ && cp tsconfig.json dist/ && cp .env dist/.

包的文件结构为:

dist
  src
    adapters
      api
        repository.d.ts
        repository.js
     ...
    application
      interfaces
        interfaceA.d.ts
        interfaceA.js
      ...
    ...
    index.d.ts
    index.js
  tsconfig.json
Run Code Online (Sandbox Code Playgroud)

哪里src/adapters/api/repository.ts

import { InterfaceA } from "application/interfaces/interfaceA";
export interface ApiRepository {
    property: InterfaceA;
    ...
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "lib": [
      "es2016" …
Run Code Online (Sandbox Code Playgroud)

typescript tsconfig npm-publish tsconfig-paths

5
推荐指数
0
解决办法
367
查看次数

如何将 tsconfig-paths 与 ts-node 一起使用

如何使用 typescript 设置路径以与 ts-node 一起运行?然后编译的时候把路径编译成绝对路径?

我有以下非常小的结构:
在此输入图像描述

koki.ts

export const calculate = (a: number, b: number) => {
  return a + b;
};
Run Code Online (Sandbox Code Playgroud)

index.ts

import { calculate } from "@koki/koki";

const result = calculate(1, 2);
console.log(result);
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
  "ts-node": {
    "transpileOnly": true,
    "require": ["tsconfig-paths/register"]
  },
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true, …
Run Code Online (Sandbox Code Playgroud)

node.js typescript tsconfig-paths

4
推荐指数
1
解决办法
1万
查看次数

更新到 27 后,Angular jest 无法理解导入路径

这是一个奇怪的错误。所以我把 jest 更新到 27 然后全部停止工作

导入路径似乎有问题。所以下面的

import { something } form 'src/app/components/.....';
Run Code Online (Sandbox Code Playgroud)

不起作用,但这确实有效:

import { something } from '../../components/....';
Run Code Online (Sandbox Code Playgroud)

我猜这是在 tsconfig 中管理的。这是我的spectsconfig:

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
        "jest", // 1
        "node",
        "Chrome"
    ],
    "esModuleInterop": true, // 2
    "emitDecoratorMetadata": true, // 3
    "allowSyntheticDefaultImports": true,
  },
  "files": ["src/test.ts", "src/polyfills.ts"],
  "include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
Run Code Online (Sandbox Code Playgroud)

和主要的 tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true, …
Run Code Online (Sandbox Code Playgroud)

typescript tsconfig jestjs tsconfig-paths angular-jest

2
推荐指数
1
解决办法
883
查看次数