小编Gae*_* C.的帖子

找不到模块:错误:将打字稿添加到 CRA 应用程序时无法解析打字稿文件

我正在尝试添加typescript到我的项目中并使用打字稿编写新组件。我按照文档进行操作,但出现此错误:
can't resolve [the typescript file]

即使是新create-react-app项目也会发生这种情况。
根据CRA 文档添加typescript到现有的 Create React App 项目,首先,我们必须typescript安装type definition files

npm install --save typescript @types/node @types/react @types/react-dom @types/jest
Run Code Online (Sandbox Code Playgroud)

快完成了!
将文件重命名js/jsxtsx并重新启动您的开发服务器!

为简单起见,我创建一个typescript名为test.tsx包含该Test组件的文件:

export default function Test(){
    return (
        <div>this is test to see typescirpt</div>
    )
}
Run Code Online (Sandbox Code Playgroud)

并将其导入main.js并渲染:

import Test from "./test";

function App() {
  return (
    <div className="App">
      <Test />
    </div>
  ); …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs webpack create-react-app

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

标签 统计

create-react-app ×1

reactjs ×1

typescript ×1

webpack ×1