升级typescript4.3.2后,不让用.ts后缀了吗?

从ts 4.1.5升级到4.3.2后跑的好好的代码,不ok了?

import api from '@/api/common.ts'代码报错

An import path cannot end with a '.ts' extension. Consider importing '@/api/common.js' instead.
image.png

这是为啥?
以下代码为:tsconfig.json

{
  "compilerOptions": {
    "target": "es2019",
    "module": "esnext",
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "jsx": "preserve",
    "incremental": true,
    "isolatedModules": true,
    "strict": true,
    "resolveJsonModule": true,
    "importHelpers": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "noUnusedLocals": false,
    "types": [
      "webpack-env",
      "jest"
    ],
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "includes": ["../shared/**/**"],
  "exclude": [
    "node_modules"
  ]
}

啥?

阅读 12.4k
2 个回答

从来没有加过 .ts,应该在很早以前就有问题的。刚试了一下,装回 TypeScript 4.1.5 仍然会提示不要加扩展名。

image.png

TypeScript 导入时不需要加扩展名,考虑到需要编译出可以用于 <script type="module" ...> 的 JS,所以允许加 .js 扩展名,这样在浏览器才不会出错。如果是打包的情况,不加扩展名比较好。

关于 Module Resolution 的问题,文档中也没有说到加 .ts 的情况。

建议还是不加吧。

不用加.ts就行吧