Reputation: 1504
New to using TypeScript with React. When I import a component that is from a .tsx file, it assumes it is a .js file by default. The error says that there is no About.js file or Contact.js file in that directory. Additionally, the TS linter won't let me add .tsx to the end of the file path. How to fix this? I used 'npx create-react-app project_name --typescript'. Here is my code
import React from 'react'
import { BrowserRouter, Route } from 'react-router-dom'
import Home from './views/Home'
import About from './views/About'
import Contact from './views/Contact'
import Navbar from './components/Navbar'
import Footer from './components/Footer'
const App: React.FC<{}> = () => {
return (
<BrowserRouter>
<Navbar title="Website" links={['About', 'Contact']} color="blue"/>
<Route exact path="/" component={Home}/>
<Route path="/home" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/contact" component={Contact}/>
<Footer/>
</BrowserRouter>
)
}
export default App
Upvotes: 15
Views: 26528
Reputation: 343
I had the same problem, I used the default create app of react (that create a JS project by default).
npx create-react-app --template typescript
Upvotes: 2
Reputation: 252
Did you use create react app? In case you didn't use or used and eject it you should add this config to your webpack config file:
{
resolve: {
extensions: [".js", ".json", ".ts", ".tsx"],
},
}
In case that you used create react app and didn't eject it you can add these rules to your typescript-eslint
module.exports = {
settings: {
'import/resolver': {
'node': {
'extensions': ['.js','.jsx','.ts','.tsx']
}
}
}
};
Upvotes: 9