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 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:
- A syntax:
let x: string = 'y';
So, why use TypeScript?
- Self documenting - By writing types you're documenting what values you expect in a function/component.
- Easier debugging - Pass a string of true when you expected a boolean? TypeScript will tell you what you did wrong.
- Simpler refactoring - Forget to rename a function or update an argument? TypeScript will tell you.
- 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.
Why not use TypeScript?
- 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.
- More/different tooling - You're adding the TypeScript transpiler to your workflow. It's what more thing to manage.
- 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)
- 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).
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
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).
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.
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.
- TypeScript Handbook - Official TypeScript Handbook. I think I'll need to have this on hand a lot in the near future...
- Using TypeScript (React Native Docs) - Official docs on how to use TypeScript in React Native.
Are you all in on TypeScript? What improvements has it made to your workflow? What about challenges?