React is a JavaScript library, and it is the most popular and industry-leading frontend development library today.
JavaScript is a loosely typed l...
For further actions, you may consider blocking this person and/or reporting abuse
Your "Do this" doesn't even compile because
Successful
,Failed
, andPending
aren't even values yet.Perhaps you were thinking
However the recommended pattern is more like this:
Objects vs Enums
Not sure what you are getting at here that can't be said about any other language? What about a world class Typescript developer over the many poor JS or JQuery developers?
Typescript is not a silver bullet but it is certainly better than a massive untyped JS project. Doesn't matter how amazing your JQuery is, because you and your team are very likely not better than a compiler.
I dunno what 'Typescript' patterns you are referring to. Abusing or overusing interfaces is something I have seen in every language that has Java/C#-like interfaces. It is hard to know what bad code you have run into, but it sounds like your problem is with developers as opposed to Typescript itself.
Imho, my react works I don't use any class component, just functional component, seems much easier.
Looks like this:
So I don't know why worst is the FC? Imho that is perfect if interface declared.
I started add typescript added our project, so actually whole JS code contain a few TS type definition - great help. Any way now, our project is state of JS - TS hybrid project, and works fine, even pipeline operator is works under TS.
I agree but in your example you should use VFC since you don't have children
I count in our working project (24k line JS/TS) contain only 4 times children use in component.
All good stuff. I don't see anything wrong with using the FunctionComponent, in fact I would strongly advocate for it especially if you are building a FunctionComponent. In fact I would say use as many built in types as possible before creating your own.
I maintain a Typescript React component library and the biggest mistake I made was not originally using the built in types for all the html components and all their attributes that React gives you for free and just extending them. I was originally creating all custom types from scratch and getting issues reported about properties not working time and time again because I had forgotten something important.
React and it's types are maintained by hundreds of developers and it's silly not to trust many eyes on it vs our own.
Why do you need
PropTypes
when coding with TypeScript? I would rather useReact.FC
and benefit from type-checking and autocomplete than not using it for the sake ofPropTypes
.very opinionated.
Why would anyone still using class components nowadays, u should refactor to functional component as they stated better to not use class component anymore and use functional instead.
Not sure all the patterns mentioned as valid for clean code. Very opinionated.
If you want 'private', use closures. Something like:
Good article, I agree with most points.
I'm also a strong believer that TypeScript helps improving the code quality and maintainability. However, regarding
4. Use type inference for defining a component state or DefaultProps
, I would say it might make it cleaner and readable, to define a single interface (or type) for the props, and after create thedefaultProps
object based on that interface. The way I do it is use a generic type, that defines a readonly type with all the properties as non-optional. These are the generic types that I use for that: gist.github.com/jbaez/71df88c47362...I recently wrote an article using those generic types, it's about separating the logic of the component from the the UI (React), which also helps making the component/screen cleaner, specially on complex ones : dev.to/jbaez/decoupling-the-logic-...
Great concept overall. But #5 is, simply put, is quite misleading.
A interface can be a type but a type cannot be an interface. A square is always a rectangle but a rectangle isn't always a square.
This article explains it well enough.
blog.logrocket.com/types-vs-interf...
I can't see the 'code below' in any of the points. Am I missing something?