r/reactjs • • Oct 10 '18

Featured Typescript - people's thoughts.

I have just started looking at typescript with react. So far it is a bit painful with all the errors, but I understand at the beginning of learning anything things can be slightly painful.

The main purpose of this post is what people here think of typescript. Is it just as useful/better to use flow/or prop types (if equivalent)? What are the pros/cons.

Also, I have been told by a colleague if you are writing your project in typescript and wish to use a third party library that isn't written in typescript this would cause errors. Is this also true?

Also, have people found that when creating a standard react setup with redux and some react dev tool extensions that this can lead to lots of errors and tends to make things a bit painful?

Also, have had issues with importing files as it says Imports should be capitalized. I assume this is a linter setting in create-react-app. Have people decided to disable certain ts linting?

14 Upvotes

37 comments sorted by

View all comments

1

u/scottbob3 Oct 10 '18

I've used Flow, TypeScript and PropTypes, of the three TypeScript is my favorite but they all have their ups and down. If you're just learning React for the first time I'd just recommend PropTypes until you really get the basics down. Once you feel comfortable with that I'd then expand into either TypeScript or PropTypes.

2

u/flanaganpete Oct 10 '18 edited Oct 10 '18

my react knowledge is ok so I feel I am ready to expand my knowledge - it just seems a bit tedious to set standard react things up from the off - one thing being react redux dev tools browser extension. Just so many errors - nearly putting me off, but I will stick at it.

2

u/scottbob3 Oct 10 '18

Can you give me an example? I don't typically have errors in my ReduxDevTools unless you're talking about like type errors caused by switching. It really is one of those things that is just kind of a pain in the ass to learn but man, it is really hard to go back. It will save your butt sooner or later.

1

u/flanaganpete Oct 10 '18

thanks! Left an example below addressing Mark's comment. Also, have had issues with importing files as it says Imports should be capitalized. I assume this is a linter setting in create-react-app. Have you decided to disable certain ts linting?

1

u/scottbob3 Oct 11 '18

Looks like /u/acemarke 's comment is spot on, personally for linters I just cheat and grab AirBnb's Eslint rules. They seems to be everyone's favorite.

2

u/acemarke Oct 11 '18

I used to like Airbnb's config, but it's gotten horribly restrictive over the last couple years as well, so I now advise against using it.

Personally, I'd suggest using eslint-config-react-app , which just warns about things that are actually likely to cause bugs, and use something like Prettier to handle any formatting concerns.

1

u/acemarke Oct 10 '18

What issues are you seeing with trying to use the Redux DevTools?

2

u/flanaganpete Oct 10 '18

Hi Mark, The code is as follows. Perhaps this works, but I am getting separate issues when I do this saying imports must be capitalized. These imports are things like `reducers` and `thunk`. I imagine I can disable these warnings, but if I start disabling lots of these warnings I then ask myself what is the point of using ts. Just seems to be errors everywhere, but as Scott says above it is kind of just a pain to learn.

const store = createStore(  
 reducers,  
  (window as any).__REDUX_DEVTOOLS_EXTENSION__ &&  
(window as any).__REDUX_DEVTOOLS_EXTENSION__(),  
 applyMiddleware(thunk)  
);

4

u/acemarke Oct 10 '18 edited Oct 10 '18

Those import warnings are not at all specific to Redux. What TypeScript setup are you using? Are you using the https://github.com/wmonk/create-react-app-typescript repo as your starting point? If so, that project has ridiculously restrictive linting rules right now, and I actually recommend disabling its linting entirely (see https://github.com/wmonk/create-react-app-typescript/issues/333 for discussion ).

Can you post your entire file as a gist and link it here, just for reference?

Also, your use of createStore() is wrong at the moment. You're passing two different store enhancers as separate arguments instead of composing them together into a single enhancer, and the devtools enhancer should always be last in that composed chain.

We actually have a new project called redux-starter-kit that is intended to help simplify several aspects of using Redux. It specifically includes a configureStore() function that does most of that store setup process for you, including setting up the DevTools Extension. I'd encourage you to try it out. I don't think we have the TS type definitions fully set up yet, but we should have those soon. Also, it's currently published on NPM as a scoped package (@acemarke/redux-starter-kit), but we were just given the actual redux-starter-kit package name and will republish it there shortly.

3

u/flanaganpete Oct 10 '18

I am about to hit the hay here so will respond in the morning with a proper answer, but just wanted to say thanks for the discussion link as it is nice to see I am not on my own with these frustratingly restrictive listing rules.

1

u/flanaganpete Oct 11 '18

I have used this setup

create-react-app hello-world --scripts-version=react-scripts-ts

1

u/acemarke Oct 11 '18

Yep, if you look at the NPM page, you can see it comes from the wmonk/create-react-app-typescript repo.

Like I said, go disable the linting (I think I had to delete something in the TS config file, or something like that), and try out our redux-starter-kit package for the store setup part.