<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Saad Sheikh on Medium]]></title>
        <description><![CDATA[Stories by Saad Sheikh on Medium]]></description>
        <link>https://medium.com/@saad27?source=rss-5e04b79f7128------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GWwkt8IXgtkBhDj6kxy_Mg.jpeg</url>
            <title>Stories by Saad Sheikh on Medium</title>
            <link>https://medium.com/@saad27?source=rss-5e04b79f7128------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:43:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saad27/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hands on TypeScript with React Native]]></title>
            <link>https://medium.com/@saad27/hands-on-typescript-with-react-native-e121b5c4cb66?source=rss-5e04b79f7128------2</link>
            <guid isPermaLink="false">https://medium.com/p/e121b5c4cb66</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react-navigation]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Saad Sheikh]]></dc:creator>
            <pubDate>Tue, 05 Jul 2022 20:35:00 GMT</pubDate>
            <atom:updated>2022-07-05T20:35:00.398Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*goDUUd_W1maXNysw-pwTSw.jpeg" /></figure><p>JavaScript being a giant in programming languages has many perks considering its vast utilization in building large scale applications for front ends and back ends but also lacks in some fields such as it mostly follows the functional approach in coding standards and not the <strong>OOP</strong> ways like other languages do such as java , C# and Go etc. It does not have a mechanism to help in type checking in compile time as well as runtime .</p><p>This is where TypeScript (created by Microsoft) came in as it provided us with ways to continue doing code in java script but also provides us with compile time type checking support (not much but its honest work) . Currently typescript is not understood by browser , backends or mobile apps its being used for actually its later <strong>transpiled</strong> into regular java script builds then executed in the platform .</p><p>Some frameworks such as Angular and Nest JS make it mandatory to utilize them with typescript while others such as React , Vue and Node JS do not but yes its an optional choice to use Typescript with them .</p><p>So in this article I will be going along with React Native (cross platform mobile application framework ) writing the basic approaches in TypeScript to point out the benefits with typed corrections and difference in compiler intellisense . The things which I will be covering are : <br>1) Component Types <br>2) Prop Types <br>3) Styles Types<br>4) Navigation Types</p><h3>Pre-requisites</h3><p>React Native CLI environment must be installed either for windows or mac os <br>resource : <a href="https://reactnative.dev/docs/environment-setup">https://reactnative.dev/docs/environment-setup</a> .<br>Preferred editor to be VS Code .<br>Having basic knowledge of React.</p><h3>Lets get to work 💻 !!</h3><p>Lets create a new TypeScript React Native project by running the following command :</p><pre>npx react-native init rntypescript --template react-native-template-typescript</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*SlL4AJy6P9aQ9eUyx1YJCw.png" /></figure><p>After creating the project and installing all the dependencies this screen should show up . <br>Lets checkout the folder structure we got 🧐 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*ldQGOt712_gmRtt1ofeB5A.png" /></figure><p>As you can see we have an App.tsx and a tsconfig.json enabling us to now make a react native app with TypeScript . Lets start by removing all the boiler code provided in the App.tsx and rendering just an empty component 🧑‍💻 .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27825ecd67d1891d8747c821a4f835de/href">https://medium.com/media/27825ecd67d1891d8747c821a4f835de/href</a></iframe><p>Lets run our app in the device and see the output (npm run android / npm run ios &amp;&amp; npm start) :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C-SFtzesOuL05AaiQVfTjQ.png" /></figure><p>App seems to be working fine now lets convert our <strong>App.tsx</strong> component into a TypeScript one :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7f59c87a9f1831e82f5b24d51d94698/href">https://medium.com/media/c7f59c87a9f1831e82f5b24d51d94698/href</a></iframe><p>As most of the experienced devs know that the<strong> React.FC</strong> is a type for React Functional Component . Before React 18 there were two namely React.VFC (Void Functional Component) which showed we do not have a usage of the <strong>children</strong> prop and <strong>React.FC </strong>(usage of children prop) . Now after React 18 we can utilize the <strong>React.FC </strong>for components having and not having the children prop .</p><p>The <strong>type</strong> denotes what type of stuff your data type will have (primitive or non primitive) for example :</p><pre>type StringOrNumber= string | number; <br>const a:StringOrNumber = 0</pre><p>This show that “a” can either contain a number or string as its assigned value . <br>Moving on as you have seen above we have defined an <strong>AppStyles </strong>types for our <strong>StyleSheet</strong> this shows what kind of styles our <strong>StyleSheet</strong> will have respective of the tags<strong> (View,Text,Image)</strong> used as React Native provided us with type declarations for its native tags . If we define an additional style without a type we get a type error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*93TTM-LRHOb8u7Q8CMUH-A.png" /></figure><h3>Deep Diving into Components 🚀</h3><p>Lets make a basic card component which will take its contents from props to get a better view of things . Make a <strong>Card.tsx</strong> file in a new components folder as shown below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*CXAWvDt5ZnpZvqJVJ1gFGg.png" /></figure><p>Our <strong>Card.tsx</strong> will have the following code :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19b3bdc40ec9c51159b4d595423b8209/href">https://medium.com/media/19b3bdc40ec9c51159b4d595423b8209/href</a></iframe><p>As you can see we have defined a type <strong>CardProps</strong> with a Name, Source &amp; Designation prop types which will be required strings in this case with a Source defined as <strong>ImageSourcePropType </strong>provided by react native to handle import of images from local directories (require or import statements) or with a network image (uri) and lastly we have a Time key which is specified optional (?) meaning that the component can have or note have that prop . With everything set lets import our component and add it to our <strong>App.tsx</strong> without the props and see what happens 😃.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*T5k0IykjFKtTW8mjWYFRuw.png" /></figure><p>As you can see we get the expected type error as we have not provided the required props to the component as defined in its type . But that’s not all lets take the keyboard pointer to the <strong>&lt;Card /&gt;</strong> and press<strong> ctrl + space.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*R0kqeT3665jkAiE5auQv2Q.png" /></figure><p>Not bad huh ? we also get the intellisense of props without the use of the famous prop-types package through typescript 🎯.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*EyR-0Ugm8iKgU0B5.jpg" /><figcaption>Man smirking 👀</figcaption></figure><p>Lets add the props on our <strong>&lt;Card /&gt; </strong>.</p><pre>&lt;Card <br>Name=&quot;Peter&quot; <br>Designation=&quot;Doctor&quot; <br>Source={require(&quot;./assets/mock.jpg&quot;)} <br>/&gt;</pre><p>Link for image: <a href="https://unsplash.com/photos/PXjQaGxi4JA">https://unsplash.com/photos/PXjQaGxi4JA</a></p><p>Our Card should look something like this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J3aqm5LZ0Zsnh0i_bwZvvw.png" /></figure><p>I guess that’s a wrap for the components part 🙌.</p><h3>Working with Navigations 🚀</h3><p>Lets install the required packages</p><pre>npm install @react-navigation/native react-native-screens react-native-safe-area-context @react-navigation/native-stack react-native-screens</pre><p>Follow the docs for configuring step only : <a href="https://reactnavigation.org/docs/getting-started/">https://reactnavigation.org/docs/getting-started/</a><br>Run the app once to make sure the packages installed correctly 💡</p><p>Lets re-organize the folder structure to use stack navigation . I will be moving all the code of <strong>App.tsx</strong> to a new File in screens / <strong>List.tsx </strong>and renaming the types and component to match <strong>List.tsx .</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*0FS2A6skmU-2N37c6-rSFg.png" /></figure><p>Now lets add a navigation folder which will have our <strong>Stack Navigator </strong>called <strong>MainStack.tsx</strong> . Our <strong>MainStack.tsx </strong>will have the following code and also will have the screen <strong>List.tsx </strong>as its screen :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17498df61d34b168440355b5c4a95256/href">https://medium.com/media/17498df61d34b168440355b5c4a95256/href</a></iframe><p>Wrap up the <strong>App.tsx</strong> with the NavigationContainer imported from <strong>@react-navigation/native</strong> so that any type of navigation can be used and add our <strong>MainStack.tsx</strong> in it as a child :</p><pre>import React from &quot;react&quot;;</pre><pre>import { NavigationContainer } from &quot;@react-navigation/native&quot;;</pre><pre>import MainStack from &quot;./navigation/MainStack&quot;;</pre><pre>const App: React.FC = () =&gt; {</pre><pre>return (</pre><pre>&lt;NavigationContainer&gt;</pre><pre>     &lt;MainStack /&gt;</pre><pre>&lt;/NavigationContainer&gt;</pre><pre>); };</pre><pre>export default App;</pre><p>All going well we should see the same screen we did before ! .<br>Lets make a second screen <strong>Detail.tsx</strong> with the following code to further see the Stack Navigation :</p><pre>import React from &quot;react&quot;;</pre><pre>import { View, Text, StyleSheet, FlexStyle, TextStyle } from <br>&quot;react-native&quot;;</pre><pre>const Details: React.FC = () =&gt; {</pre><pre>return (</pre><pre>&lt;View style={styles.container}&gt;</pre><pre>     &lt;Text style={styles.text}&gt;Details Screen&lt;/Text&gt;</pre><pre>&lt;/View&gt;   <br>);  };</pre><pre>type DetailStyles = {</pre><pre>container: FlexStyle;</pre><pre>text: TextStyle;  };</pre><pre>const styles = StyleSheet.create&lt;DetailStyles&gt;({</pre><pre>container: {</pre><pre>flex: 1,</pre><pre>justifyContent: &quot;center&quot;,</pre><pre>alignItems: &quot;center&quot; },</pre><pre>text: {</pre><pre>fontSize: 20,</pre><pre>color: &quot;black&quot; }</pre><pre>});</pre><pre>export default Details;</pre><p>Adding the <strong>Details.tsx</strong> screen to our <strong>MainStack.tsx :</strong></p><pre>import Details from &quot;../screens/Details&quot;;</pre><pre>///...<br>&lt;Stack.Screen name={&quot;List&quot;} component={List} /&gt;</pre><pre>&lt;Stack.Screen name={&quot;Details&quot;} component={Details} /&gt;<br>...///</pre><p>We want to navigate to the <strong>Details.tsx</strong> screen for this case as the List.tsx is now inside a Stack navigator it will be getting a navigation prop which we will use to navigate to the screen . Lets wrap the <strong>&lt;Card/&gt;</strong> in the <strong>List.tsx</strong> with <strong>TouchableOpacity</strong> do give it an onPress function :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18e5055771959bcac6cfab6aa9aa2e11/href">https://medium.com/media/18e5055771959bcac6cfab6aa9aa2e11/href</a></iframe><p>Lets see the result :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PT16uF78GdwrI4ZagiphdQ.gif" /></figure><p>Moving on as we know that this is just a simple navigation to a screen but what if we want to send some data from one screen to another we would use the <strong>navigation.navigate(“screen_name”,{…some_values})</strong> . Yes this solves our problem but how would we describe to this function what values the next screen expects or more importantly the<strong> type </strong>and format of values we are suppose to send to the screen to keep the screen’s working in tack .</p><p>Lets create a parameters <strong>type</strong> for our <strong>MainStack</strong> and define the type of data which every screen will require in it :</p><pre>export type MainStackParams = {</pre><pre>List: undefined;</pre><pre>Details: { Id: string | number };</pre><pre>};</pre><p>This shows that the <strong>Details</strong> screen requires an <strong>Id</strong> param when navigating to it. Lets use them to make a prop type for our<strong> List.tsx </strong>screen . Import :</p><pre>import { NativeStackNavigationProp } from <br>&quot;@react-navigation/native-stack&quot;;<br>import { MainStackParams } from &quot;../navigation/MainStack&quot;;</pre><pre>type ListProps = {</pre><pre>navigation: NativeStackNavigationProp&lt;MainStackParams&gt;;</pre><pre>};</pre><p>I defined the prop types for <strong>List.tsx</strong> as <strong>&lt;any&gt; </strong>earlier now after adding the <strong>ListProps</strong> type the code for <strong>List.tsx</strong> will look like :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a768c5da714af540d7c3f932ca99dfc/href">https://medium.com/media/2a768c5da714af540d7c3f932ca99dfc/href</a></iframe><p>Take a look at <strong>navigation.navigate(“Details”) </strong>as it must be giving a type error<strong> </strong>😁</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*lDAaIYHEUu9gynkiMCs-yA.png" /></figure><p>This is because we have defined Details to have a type <strong>{Id:string|number} </strong>so now we have a type of <strong>params </strong>to send every time we navigate to “Details” screen while residing in the MainStack . Yes these <strong>params</strong> only apply to the <strong>MainStack </strong>, when we use <strong>“Details”</strong> screen in a different Stack it will require <strong>params</strong> according to it and not the <strong>MainStack . </strong>Also we need to add the same navigation type for the <strong>“Details”</strong> screen as it is in the same stack .</p><p>Fixing the type error by providing its required stuff 💡</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*rV4TrxYuqRNzhlD0qa7waA.png" /></figure><p>Now lets get this value in the detail screen . We will de-structure the route key from props 🧑‍💻 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*mQK_PYcHVWxYiSBnz1Uojg.png" /></figure><p>Oops as route key is also coming from props because it is inside a stack navigation we will need to specify a type for it : <br>Import</p><pre>import { RouteProp } from &quot;<a href="http://twitter.com/react">@react</a>-navigation/native&quot;;</pre><p>Lets use it to make a type for the route key which is coming in props :</p><pre>type DetailProps = {</pre><pre>navigation: NativeStackNavigationProp&lt;MainStackParams&gt;;<br>route: RouteProp&lt;MainStackParams, &quot;Details&quot;&gt;;</pre><pre>};</pre><p>The <strong>RouteProps</strong> expects first argument to be the <strong>params </strong>type and the second the screen name . Lets check on that type error we were getting on the <strong>route</strong> key prop .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*IZG1a53t1W5ZT50MN-caNA.png" /></figure><p>Looks good now for safety lets try to de-structure some random key from the route params :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*vCI7-Fqp7r-9JWzWg-WsLw.png" /></figure><p>That’s how you add types for basic navigation flows 🚀</p><h3>Conclusion :</h3><p>We have gone through basic usage of TypeScript with React Native covering some fundamentels such as prop types , navigations and style types with little practicle examples . Hoping you found this article insightful 🙌 .</p><h3>Resources :</h3><p><a href="https://reactnative.dev/docs/typescript">https://reactnative.dev/docs/typescript</a><br><a href="https://reactnavigation.org/docs/typescript/">https://reactnavigation.org/docs/typescript/</a><br>Source Code : <a href="https://github.com/saad277/TypeScript-ReactNative-Medium">https://github.com/saad277/TypeScript-ReactNative-Medium</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e121b5c4cb66" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>