Title.
Pros and Cons?
Anything you care about should be written in typescript. There’s no legitimate debate here.
Pros: Catches many compile time errors, vastly reducing the number of errors a developer can make.
Improved intellisense. Better autocomplete. Devs can write code faster because VSCode is able to help them out a lot more.
Easier refactoring. For example If you decide to change some data type used throughout your application, you can instantly know each part of the application that needs to be changed because typescript will produce errors in each file.
It vastly improves maintainability.
Cons:
overhead in writing types. Although, the amount of time saved is orders of magnitude greater than the time it takes to write these types
So yeah. It’s not a debate. If you care about some piece of code you should use typescript. End of story
Cons:
-
One more tool in your toolchain.
-
Some code, especially libraries, can be exceptionally hard to type.
-
Some people "overtype" and lose productivity, some people "undertype" making TypesSript less effective. Finding the right balance is mental overhead.
-
Almost all third-party libraries have types but type quality can vary.
-
No runtime checks, leading to false sense of correctness
-
Longer build times, some types are very expensive to compute and type checking is not easy to debug.
Pros:
-
Anecdotal, but TypeScript has found and prevented more bugs in our codebase than any unit test suite could ever do
-
Better intellisense, JSDoc can do some of this but TypeScript is way more powerful
-
Some JavaScript anti patterns, e.g. god object, global state or mutability, are naturally disincentivized because they are hard to type leading to better code
-
TypeScript can be used in CI/CD
Even though I wrote more cons, the pros, especially the first one, way outweigh the cons for anything half serious. Writing TypeScript WILL make you a better programmer and it WILL improve your programs.
React is a library/framework for building UIs. You construct various components that describe what you want the page to look like, and then react handles figuring out what changed and making updates to the page. If someone just says "react" without any other context, this is probably what they mean.
React Native uses the same core functionality as react, but once it has figured out the changes that need to be made, rather than update the dom (ie, the webpage), it updates native components for android or ios. React native thus lets you write native phone apps, using the syntax and tools that are familiar to react developers.
Javascript vs typescript is completely different axis. Javascript is the main programming language used by webpages. Typescript is a superset of javascript, which lets you add type information to your code. This then lets you find bugs in your code quicker, because your IDE and build process can check the types to see if you've made mistakes. You can write a webpage (including a react webpage) using whichever you prefer (I prefer typescript).
Before going into the difference between React JS and React TS, you have to understand how React TS projects, in fact any TS project, actually works.
TypeScript is not a language that runs natively on the web. The typings that you add to your codebase don't have any meaning on the browser. When you build your code, your TypeScript code gets transpiled to JavaScript. So if you look at the bundled code, you can't find any difference between React JS and React TS, they bundle the same JavaScript code.
With that out of the way, let's talk about React Native and React. React, on its own, is a framework to build applications declaratively with components. When people refer to React while building a web app, they often just say React but they should instead say React and React-DOM because React-DOM is the library that actually renders to the browser.
You can think of React as the foundations of the React ecosystem. React-DOM is the library that builds on React to render your applications on the web. React Native is the library that renders your application on mobile apps and even more (MacOS, Windows, Apple TV and so on are also possible compile targets for React Native). I suggest you read React Native documentation to learn more about how it does this. I've personally used the Expo Managed Workflow to develop cross-platform mobile apps.
The difference between doing React JS and React TS is the language you write. You write JavaScript or TypeScript, but at the end of the day the bundle is always JavaScript (Note: Deno is different, it natively runs TypeScript, check it out).
And the difference between writing Javascript and Typescript is only the developer experience. When you write Typescript, you get static type-checking and incredible autocompletion. I always write Typescript because it finds most mistakes I make before I get a chance to even run my code. The downside to this is that, when I use libraries that don't have typings, I have to deal with that on my own (either write typings myself or use something else).
import * as React vs. import React
It just depends on your tsconfig file. There is an option called allowSyntheticDefaultImports. If that is set to true, you can do “import React from ‘react’”
From typescript docs:
allowSyntheticDefaultImports
Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
More on reddit.comZustand React Typescript Tutorial (vs Easy Peasy)
I made a couple of TypeScript React VS Code Snippets.. are they best practice?
I prefer to set the state directly on the class as a property - in the transpiled code, this happens after the super(props) call, so you can still use this.props if you need something off of it for initial state.
This makes the constructor totally unnecessary in most cases, unless you need to do other stuff e.g. function binding.
More on reddit.comReact using TypeScript vs using JavaScript?
Cons:
-
One more tool in your toolchain.
-
Some code, especially libraries, can be exceptionally hard to type.
-
Some people "overtype" and lose productivity, some people "undertype" making TypesSript less effective. Finding the right balance is mental overhead.
-
Almost all third-party libraries have types but type quality can vary.
-
No runtime checks, leading to false sense of correctness
-
Longer build times, some types are very expensive to compute and type checking is not easy to debug.
Pros:
-
Anecdotal, but TypeScript has found and prevented more bugs in our codebase than any unit test suite could ever do
-
Better intellisense, JSDoc can do some of this but TypeScript is way more powerful
-
Some JavaScript anti patterns, e.g. god object, global state or mutability, are naturally disincentivized because they are hard to type leading to better code
-
TypeScript can be used in CI/CD
Even though I wrote more cons, the pros, especially the first one, way outweigh the cons for anything half serious. Writing TypeScript WILL make you a better programmer and it WILL improve your programs.
More on reddit.com