DEV Community

Cover image for Should You Use TypeScript with React Native? [2021]
Spencer Carli
Spencer Carli

Posted on • Originally published at reactnativeschool.com

Should You Use TypeScript with React Native? [2021]

If you've spent much time looking around the React/React Native bubble you've likely heard of TypeScript (if not no worries - we'll discuss it more here).

I know that I have and, honestly, I've all but ignored it up to this point.

But I'm examining the tools I use in my React Native apps to see what I can change/improve.

One of those is TypeScript. Tons of people love it.

So let's take a look.

This is just my initial analysis as I investigate whether I want to invest in TypeScript or not. If I'm wrong on something I would love to learn more. Let me know on Twitter.

What is TypeScript?

TypeScript adds typing to JavaScript.

What is typing? It's a means to declare the type of a value/function/etc.

This means that, by declaring the expected type, we can determine if the value we're passing to a function is valid or not. This allows us to find errors earlier (compile time) vs. when we run the app (run time).

TypeScript has two parts:

  1. A syntax: let x: string = 'y';
  2. A transpiler: takes the TypeScript and converts it into standard JavaScript

Pros of TypeScript in React Native

So, why use TypeScript?

  1. Self documenting - By writing types you're documenting what values you expect in a function/component.
  2. Easier debugging - Pass a string of true when you expected a boolean? TypeScript will tell you what you did wrong.
  3. Simpler refactoring - Forget to rename a function or update an argument? TypeScript will tell you.
  4. Let your text editor/IDE help you more - TypeScript has integration with just about all editors. This allows your editor to, using TypeScript definitions, autocomplete and tell you what to pass to a function/component.
  5. Add structure to larger apps - By defining how your app works you're forced to think about your app in context of the rest of the app. Also you can avoid "traps" that you can fall into with JavaScript (looking at you global scope and this)
  6. Guaranteed valid JS - If you have valid TypeScript you know you'll get valid JavaScript output that will run at whatever target level you specify. By writing straight JavaScript (that isn't transpiled) you don't have that guarantee.
  7. Can be gradually adopted - This is a big one for me. You don't have to write 100% TypeScript. Valid JavaScript is still valid TypeScript so you don't need to jump into it 100% at the beginning. You're able to test the waters.

Cons of TypeScript in React Native

Why not use TypeScript?

  1. Another dependency to manage - If you add a dependency you have to manage it, understand it's configuration, and keep it updated. Nothing is free.
  2. New/different syntax - It's (kind of) like a new language. If you're proficient with writing JavaScript you'll have to be patient while you get up to speed with the new syntax.
  3. More/different tooling - You're adding the TypeScript transpiler to your workflow. It's what more thing to manage.
  4. Move slower - you get a lot of benefits by typing your code but you still have to write those types. That will slow you down initially. But it's also got big benefits (see pros)
  5. Third party package types - This is kind of an issue but not a big one. How do you get types for third party packages? Some packages have them included (such as React Navigation) others you'll have to add another dependency. Others just don't have them (but TypeScript can infer things automatically).
  6. Talent pool? - This isn't really a good point but I wanted to mention it. More people use JavaScript than TypeScript. But someone who knows JavaScript can learn TypeScript (and it's constantly growing its user base).
  7. Transpiling is an extra step - I wanted to add this but it's not really a valid point because, in React Native, Babel is already transpiling our JavaScript (and Babel will continue to transpile TypeScript).

How to Use TypeScript in a React Native App

So, I've decided I want to create a React Native app and use TypeScript. How do I do it?

npx react-native init MyApp --template react-native-template-typescript
Enter fullscreen mode Exit fullscreen mode

Yeah, that's it... The React Native community has setup a template to use TypeScript in React Native.

If you're using Expo just select one of their TypeScript templates when setting up your project.

Initial setup on a new app is definitely not a reason to avoid using TypeScript in React Native.

The React Native Docs have an entire page dedicated to using TypeScript in React Native (including how to add it to an existing project).

Final Thoughts

Look, I'll be honest with you, I'm a lazy programmer.

I'm a firm believer of "if it ain't broke, don't fix it."

And, generally, I'm happy with my workflow.

BUT...

I think I've put off this decision for a long enough and just reading about TypeScript isn't enough. I want to use it in a real project.

So the next major app I work on (looking at a large trivia app example for React Native School) I'm going to use TypeScript to get a better grasp on it.

Resources


Are you all in on TypeScript? What improvements has it made to your workflow? What about challenges?

Top comments (0)