<?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>DEV Community: olivertembo</title>
    <description>The latest articles on DEV Community by olivertembo (@olivertembo).</description>
    <link>https://dev.to/olivertembo</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%2F658565%2Fc75c82d2-f0e6-4525-83b5-8c187d172875.png</url>
      <title>DEV Community: olivertembo</title>
      <link>https://dev.to/olivertembo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olivertembo"/>
    <language>en</language>
    <item>
      <title>React, Typescript with Laravel</title>
      <dc:creator>olivertembo</dc:creator>
      <pubDate>Fri, 26 Nov 2021 10:38:13 +0000</pubDate>
      <link>https://dev.to/olivertembo/react-typescript-with-laravel-3f20</link>
      <guid>https://dev.to/olivertembo/react-typescript-with-laravel-3f20</guid>
      <description>&lt;p&gt;Sometimes it is much more convenient to build a monolith up.&lt;br&gt;
I will take you through how I setup my Laravel app with React with Typescript.&lt;/p&gt;

&lt;p&gt;For this we will make a simple Laravel, React, Typescript App. We will call it. Laravel-Guava&lt;br&gt;
Technology Stack:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PHP/Laravel&lt;/li&gt;
&lt;li&gt;Inertia/Breeze&lt;/li&gt;
&lt;li&gt;React/Typescript&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am using a windows with WSL, ubuntu for this. using a mac should be the same experience. 😉 &lt;/p&gt;

&lt;p&gt;Feel free to fork it make improvements as you like.&lt;/p&gt;

&lt;p&gt;Technology: Make sure you have composer, typescript, node, docker installed. If not I'll add a few lines to make it work.&lt;/p&gt;

&lt;p&gt;Step One: Install the latest version of Laravel and run it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl -s https://laravel.build/&amp;lt;name-of-your-app&amp;gt; | bash&lt;/code&gt;&lt;br&gt;
 &lt;code&gt;./vendor/bin/sail up&lt;/code&gt; later &lt;code&gt;composer-docker up&lt;/code&gt; is enough&lt;br&gt;
&lt;code&gt;npm install&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm run install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Initialize typescript in your project. this will create a ts config file. If that doesn't work, install typescript here[&lt;a href="https://www.typescriptlang.org/download"&gt;https://www.typescriptlang.org/download&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx tsc --init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;these are my configuration for my typescript&lt;br&gt;
&lt;code&gt;{&lt;br&gt;
  "compilerOptions": {&lt;br&gt;
    "target": "es5",&lt;br&gt;
    "module": "es2020",&lt;br&gt;
    "moduleResolution": "node",&lt;br&gt;
    "baseUrl": "./",&lt;br&gt;
    "strict": true,                 // Enable strict type-checking options&lt;br&gt;
    "skipLibCheck": true,           // Skip type checking of declaration files&lt;br&gt;
    "noImplicitAny": false          // Bypass raising errors on&lt;/code&gt;any&lt;code&gt;type&lt;br&gt;
  },&lt;br&gt;
  "include": ["resources/js/**/*"]  // Frontend paths pattern&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install the dependencies&lt;br&gt;
npm install ts-loader typescript --save-dev&lt;br&gt;
composer require laravel/breeze --dev&lt;br&gt;
php artisan breeze:install react&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure Laravel Mix&lt;br&gt;
Initial Laravel installation comes with a boilerplate JavaScript entry point, which needs to get converted into TypeScript. All you need is to rename .js to .ts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
-resources/js/app.js&lt;br&gt;
+resources/js/app.tsx&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;configure  webpack.config.js to resolve typescript extensions&lt;br&gt;
`&lt;br&gt;
const path = require('path');&lt;/p&gt;

&lt;p&gt;module.exports = {&lt;br&gt;
    resolve: {&lt;br&gt;
        alias: {&lt;br&gt;
            '@': path.resolve('resources/js'),&lt;br&gt;
        },&lt;br&gt;
        // Add &lt;code&gt;.ts&lt;/code&gt; and &lt;code&gt;.tsx&lt;/code&gt; as a resolvable extension.&lt;br&gt;
        extensions: [".ts", ".tsx", ".js"]&lt;br&gt;
    },&lt;br&gt;
};&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;Then, let Mix know that it should handle the JavaScript code as TypeScript. Laravel Mix comes with built-in TypeScript support.&lt;/p&gt;

&lt;p&gt;webpack.mix.js&lt;/p&gt;

&lt;p&gt;-mix.js('resources/js/app.js', 'public/js')&lt;br&gt;
+mix.ts('resources/js/app.ts', 'public/js')&lt;/p&gt;

&lt;p&gt;That is it; you are all set! You can keep writing code the way you used to and utilize some TypeScript features and improve your front-end experience.&lt;/p&gt;

&lt;p&gt;Run npm install for any additioal mix dependancies&lt;/p&gt;

&lt;p&gt;npm run watch&lt;/p&gt;

&lt;p&gt;link to repo: &lt;a href="https://github.com/olivertembo/laravel-react-typescript-inertia-boilerplate"&gt;https://github.com/olivertembo/laravel-react-typescript-inertia-boilerplate&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>react</category>
      <category>typescript</category>
      <category>inertia</category>
    </item>
  </channel>
</rss>
