-
Create a new React app without TypeScript template: npx create-react-app my-first-app cd my-first-app
-
Install necessary TypeScript packages: npm install --save typescript@4.9.5 @types/node@18.11.0 @types/react@18.0.0 @types/react-dom@18.0.0 @types/jest@28.1.6
-
Add TypeScript configuration: Create a
tsconfig.jsonfile in the root of your project with the following content: { "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"] } -
Rename files to TypeScript:
- Rename
src/index.jstosrc/index.tsx - Rename
src/App.jstosrc/App.tsx
- Rename
-
Install necessary React packages: npm install --save react@18.2.0 react-dom@18.2.0 @testing-library/react@13.4.0 react-scripts@latest
-
Install web-vitals as a development dependency: npm install --save-dev web-vitals
-
App.tsx: import React from 'react'; import logo from './logo.svg'; import './App.css';
const App: React.FC = () => { return (
); }export default App;
-
Index.tsx: import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement); root.render( <React.StrictMode> </React.StrictMode> );
// If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals reportWebVitals();
-
Create a declaration file: react-app-env.d.ts /// declare module "*.svg" { const content: string; export default content; }
-
Run the TypeScript compiler to check for any issues: npx tsc --noEmit
-
Start your development server: npm start
-
Install Test Package npm install @testing-library/jest-dom
-
Install Lastes Comand after Edit All of js to ts file npm install @testing-library/jest-dom@^5.16.5 @testing-library/react@^13.4.0 @testing-library/user-event@^13.5.0 @types/node@^16.18.23 @types/react@^18.0.28 @types/react-dom@^18.0.11 react@^18.2.0 react-dom@^18.2.0 react-scripts@5.0.1 web-vitals@^2.1.4
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|