create-react-app 在Visual Studio Code中,导入路径不能以'.tsx'扩展名结尾,

hxzsmxv2  于 2个月前  发布在  React
关注(0)|答案(7)|浏览(41)

描述问题

我可以使用

// @ts-ignore
import ReloadWrapper from "../Others/ReloadWrapper.tsx";

运行我的项目,但无法在Visual Studio Code中享受Typescript的类型提示。如果我去掉@ts-ignore,就可以恢复正常:

当我使用

import ReloadWrapper from "../Others/ReloadWrapper";

时,Webpack无法识别导入:

你是否尝试恢复依赖项?

是的

你在用户指南中搜索了哪些术语?

  • 导入路径不能以'.tsx'扩展名结尾

环境

Environment Info:

  current version of create-react-app: 5.0.1
  running from C:\Users\jy95\AppData\Local\npm-cache\_npx\c67e74de0542c87c\node_modules\create-react-app

重现问题的步骤

  1. Visual Studio Code版本为1.68.1
  2. Git克隆https://github.com/jy95/jy95.github.io.git
  3. 在某个地方(例如import ReloadWrapper from "../Others/ReloadWrapper.tsx";)删除一个// @ts-ignore
  4. 完成

预期行为

没有错误

实际行为

导入路径不能以'.tsx'扩展名结尾

可复现的演示

https://github.com/jy95/jy95.github.io master分支

cczfrluj

cczfrluj1#

如果你还没有解决这个问题,我可以通过在根目录下添加一个tsconfig.json文件并去掉import的扩展名来解决这个问题。
我采取的解决步骤如下:

  • 全局安装typescript:npm install typescript -g
  • 初始化tsc:tsc --init
0md85ypi

0md85ypi2#

这解决了我的问题。值得注意的是,指南中提到了 You are not required to make a [tsconfig.json file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html), one will be made for you. You are allowed to edit the generated TypeScript configuration.,这可能是在参考从头开始创建应用程序,但由于页面被称为“添加Typescript”,并且还涵盖了将其添加到现有项目中,因此可能会令人困惑。
您还需要在 src/ 目录下添加一个名为 'react-app-env.d.ts' 的文件,并包含以下内容:

/// <reference types="react-scripts" />

这通常也是由 create-react-app 为您创建的。它允许像导入svg模块这样的操作。

pbossiut

pbossiut3#

我创建了一个PR来更新指南,希望能更清楚。

mwecs4sa

mwecs4sa4#

大家好!我刚刚通过使用tsconfig.json解决了这个问题。在创建了tsconfig.json文件之后,你只需要粘贴以下设置:

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

然后,你就可以在不使用'tsx'扩展名的情况下导入组件了。

yhived7q

yhived7q5#

大家好!我刚刚通过使用tsconfig.json解决了这个问题。在创建了tsconfig.json文件之后,你只需要粘贴以下设置:

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

然后,你就可以在不使用'tsx'扩展名的情况下导入组件了。非常感谢,这对我来说很有效。之前一直困扰了我很久。

8hhllhi2

8hhllhi26#

如果你还没有解决这个问题,可以通过在根目录下添加一个tsconfig.json文件并去掉import语句中的扩展名来解决。
我采取的解决步骤如下:

  • 全局安装typescript:npm install typescript -g
  • 初始化tsc:tsc --init

如何避免使用扩展名解决问题?假设我们有相同名称但扩展名不同的文件,例如sample.js和sample.ts,似乎我们需要更新tsconfig/webpack配置。谢谢

o7jaxewo

o7jaxewo7#

大家好!我刚刚通过使用 tsconfig.json 解决了这个问题。在创建了 tsconfig.json 文件之后,你只需要粘贴以下设置:

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

然后,你可以在不使用 'tsx' 扩展名的情况下导入组件。对于 .jsx 和 .tsx 文件也是如此,跳过扩展名只会解决问题,而不会真正解决问题。参考评论:#12527(评论)

相关问题