TypeScript src 模块导入在 dist 中未正确编译

问题描述 投票:0回答:1

我正在 TS 中开发一个 REST API,总结起来,其结构如下:

├── dist
│   ├── index.js
│   ├── library.js
├── src
│   ├── index.ts
│   ├── library.ts
├── node_modules
├── package.json
├── package-lock.json
├── tsconfig.json
└── .gitignore

我面临的问题是index.ts中的模块导入没有在index.js中正确编译,作为参考:

索引.ts

import { someFunc } from '../library.ts';

index.js

import { someFunc } from '../library.ts';

如果文件编译正确,index.js 的导入将是:

import { someFunc } from '../library.js';

我认为可以肯定地说该问题与 tsconfig.json 有关:

{
  "compilerOptions": {
    "module": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "outDir": "dist",
    "allowImportingTsExtensions": true
  }
}

我尝试使用 tsconfig.json 但最终没有找到令人满意的解决方案,我还想将模块类型设置为

NodeNext
而不是
ESNext

typescript module tsconfig
1个回答
0
投票

tsc
不触及导入路径(又名“模块说明符”),包括潜在的文件扩展名

模块说明符(您

from
或传递给
import
的字符串
require
)始终按原样发出。

您必须编写final文件扩展名:

// In index.ts
import { someFunc } from '../library.js'; // Final .js file extension

...即使这意味着

在输入文件中看到输出文件扩展名很奇怪

© www.soinside.com 2019 - 2024. All rights reserved.