我正在尝试添加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/jsx为tsx并重新启动您的开发服务器!
为简单起见,我创建一个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)