<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Igor Filchev</title>
    <description>The latest articles on Forem by Igor Filchev (@filchevigor).</description>
    <link>https://forem.com/filchevigor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F917807%2F16528103-fd3c-490b-b15b-eab4bfe0e994.jpeg</url>
      <title>Forem: Igor Filchev</title>
      <link>https://forem.com/filchevigor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/filchevigor"/>
    <language>en</language>
    <item>
      <title>Adding typescript to the existing react project</title>
      <dc:creator>Igor Filchev</dc:creator>
      <pubDate>Tue, 30 Aug 2022 09:07:00 +0000</pubDate>
      <link>https://forem.com/filchevigor/adding-typescript-to-the-existing-react-project-19hn</link>
      <guid>https://forem.com/filchevigor/adding-typescript-to-the-existing-react-project-19hn</guid>
      <description>&lt;p&gt;This note will help you understand how to add typescript to an existing Create React App project and start writing new files in the typescript. I use npm as the package manager, but it will not be a problem for you to use yarn or some other way.&lt;/p&gt;

&lt;p&gt;If you do not use Redux, do only steps numbered as 1, 2 and 7 only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main steps
&lt;/h3&gt;

&lt;p&gt;1) Let’s install required packages via terminal[1]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save typescript @types/node @types/react 
npm i --save @types/react-dom @types/jest
npm install --save-dev @tsconfig/node16
npm i @typescript-eslint/parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Create tsconfig.json file manually or with terminal command and fill it with my example of the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
 "$schema": "https://json.schemastore.org/tsconfig",
 "display": "Node 16",
 "extends": "@tsconfig/node16/tsconfig.json",
 "compilerOptions": {
   "target": "ES2022",
   "lib": [
     "dom",
     "dom.iterable",
     "ES2022"
   ],
   "allowJs": true,
   "skipLibCheck": true,
   "esModuleInterop": true,
   "allowSyntheticDefaultImports": true,
   "strict": true,
   "forceConsistentCasingInFileNames": true,
   "noFallthroughCasesInSwitch": true,
   "module": "es2022",
   "moduleResolution": "node",
   "resolveJsonModule": true,
   "isolatedModules": true,
   "noEmit": true,
   "jsx": "react-jsx",
   "typeRoots": [
     "src/@types"
   ]
 },
 "include": [
   "src"
 ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$schema&lt;/code&gt;, &lt;code&gt;display&lt;/code&gt; and &lt;code&gt;extends&lt;/code&gt; do not set anything to the tsconfig file. They only lead to the online list of options with values, which can be present in the tsconfig.json. In other words, you will have to add configuration by looking at the scheme[2][3].&lt;/p&gt;

&lt;p&gt;My IDE changes module from the &lt;code&gt;es2022&lt;/code&gt; to &lt;code&gt;esnext&lt;/code&gt;. Do not worry, if you will see this in the notification panel of your IDE.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;typeRoots&lt;/code&gt; contain a path to the file with the default useSelector app state (explanation you can find later). Erase typeRoots from the file, if you do not use Redux.&lt;/p&gt;

&lt;p&gt;3) Skip this step, if you do not use Redux. &lt;/p&gt;

&lt;p&gt;It is time to change react-redux index filename extension from &lt;code&gt;.js&lt;/code&gt; to &lt;code&gt;.ts&lt;/code&gt; (your file can have another name). After that replace&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default combineReducers({
   ...yourReducers
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const reducers = combineReducers({
   ...yourReducers
})

export type AppState = ReturnType&amp;lt;typeof reducers&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation of the AppState purpose will be later&lt;/p&gt;

&lt;p&gt;4) This section is needed for the developers, which utilize Redux. &lt;/p&gt;

&lt;p&gt;Modify file &lt;code&gt;store.js&lt;/code&gt; (your implementation file can have another name). You will have to replace&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import reducers from './reducers';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { reducers } from './reducers/index.ts';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5) Please, ignore this step, if you do not use redux. &lt;/p&gt;

&lt;p&gt;And current step requires creation of the file &lt;code&gt;react-app-env.d.ts&lt;/code&gt; (&lt;code&gt;src/react-app-env.d.ts&lt;/code&gt;)[4]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// &amp;lt;reference types="react-scripts" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6) Well, this file is not needed for the not-redux users&lt;/p&gt;

&lt;p&gt;Add file under path &lt;code&gt;src/@types/react-redux.d.ts&lt;/code&gt; with next code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'react-redux';

import { AppState } from '../reducers';

declare module 'react-redux' {
   interface DefaultRootState extends AppState {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite AppState being not used anywhere else, it allows not to import reducers app state in the useSelector method every time and the method always know the it’s argument type return value[5]. &lt;/p&gt;

&lt;p&gt;7) Well, modify ESlint file as the last step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change parser
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"parser": "@typescript-eslint/parser",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change parser options
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"parserOptions": {
   "ecmaVersion": "latest",
   ...rest code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Update env
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"env": {
   "es2021": true,
   ...rest code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Modify rules for disabling warnings or errors for unused code (by your choice only), removing mistake about typescript file extensions (code will work without this rule, but you will see unpleasant message) and remove warning about lack of default props
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"rules": {
   "no-use-before-define": "off",
   "react/jsx-filename-extension": [2, { "extensions": [".js", ".jsx", ".ts", ".tsx"] }],
   "react/require-default-props": "off"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I have to warn you, that file import with .ts and .tsx extensions is obligatory now (you can ignore it only in the created by create-react-app application). As a disadvantage, live reloading can be broken and you will have to reload the page manually every time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;p&gt;[1] &lt;a href="https://create-react-app.dev/docs/adding-typescript/"&gt;Adding TypeScript&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#tsconfig-bases"&gt;What is a tsconfig.json&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://json.schemastore.org/tsconfig"&gt;tsconfig.json base configuration&lt;/a&gt;&lt;br&gt;
[4] &lt;a href="https://stackoverflow.com/questions/67262914/what-is-the-react-app-env-d-ts-in-a-react-typescript-project-for"&gt;What is the react-app-env.d.ts in a react typescript project for&lt;/a&gt;&lt;br&gt;
[5] &lt;a href="https://stackoverflow.com/questions/60777859/ts2339-property-tsreducer-does-not-exist-on-type-defaultrootstate"&gt;TS2339: Property 'tsReducer' does not exist on type 'DefaultRootState'&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
