【问题标题】:Can't find module TS2307 in React在 React 中找不到模块 TS2307
【发布时间】:2019-11-01 23:13:05
【问题描述】:
import React from "react";
import logo from "./logo.svg";
import text from "./compiler/test/index.txt";
import "./App.css";

const App: React.FC = () => {
  // const compiler = new Compiler("../test/index.jack");
  // compiler.save();
  console.log("text: ", text);

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
};

export default App;

我正在使用 react-app-rewired 来覆盖 webpack 配置,而不是弹出。 该组件读取一个.txt文件并记录其内容,但是在导入的第三行有一个错误Cannot find module './compiler/test/index.txt'. TS2307

【问题讨论】:

  • 这可能只是因为类型.txt 没有在任何打字稿定义中定义。导入有用吗?如果是这样,您可能只需要在某处定义类型。例如,在根文件夹中的 global.d.ts 中。你可以在这里查看答案:stackoverflow.com/questions/47122504/…
  • 谢谢@Daniel。带有 typescript 项目的 create-react-app 默认有一个名为 react-app-env.d.ts 的文件,这是声明新类型的地方。

标签: reactjs webpack


【解决方案1】:

由于我的评论似乎解决了这个问题,这里作为官方回答:

您可能只需要在打字稿定义中的某处声明类型.txt。对于 create-react-app,这将在 react-app-env.d.ts 中。例如:

declare module '*.txt' {
  const content: string;
  export default content;
}

【讨论】:

    猜你喜欢
    • 2017-03-09
    • 2022-11-02
    • 2019-11-29
    • 2017-10-13
    • 2017-09-27
    • 1970-01-01
    • 2017-02-02
    • 2017-09-17
    • 2021-03-16
    相关资源
    最近更新 更多