Let me give you an example. When you start typing, your first React component, you learn, okay, there's React.function component, and you do it like this, and then realize, okay, but people don't use this. People use React.fc. And I was like, we don't have fun, funky, and funny for function as a keyword in JavaScript. Why do this? Why can't we have one way of doing things so you can teach one way, and you can learn one way, and you can be good with it? But this is not the end of the story because then, when you actually start to like read about how to type components, and so on and so forth, you realize a lot of experts, TypeScript experts, tell you like don't do React.fc.
So basically, and it all comes down to like mostly all of them complaining about one issue that existed in, yeah, spoiler alert, it's solved now. Basically, the types always implicitly, so this is what kind of happened, the implicities of React.fc added the implicit children. So if you had a component, like a model, that when you really need to have children, we expect them, you could pass them in or you could leave them out. But if you have other components where you don't even make use of children at all, you cannot declare it in any way because it will always be optionally there with React.fc. And so basically, what they proposed is use this instead. And you do this and you migrate your whole code base with your team, and then you onboard new juniors and they're really, really excited because they feel like, hey, they have an improvement that they can tell you about. Did you know there is a React.fc and we could migrate to this? And the only reaction is like, let's talk about something else. And you feel really bad for them. But it all changed. So by the way, the React types are not maintained by the React core team. But whoever is maintaining them, they made a really great decision because with React 2018 and breaking change, they sneaked in a change with their types, removing the implicit children. Ta-da. And I was like, hallelujah, solved all my problems, I can go back to React.fc. Why? Because if I have this here, if I have children, I have to now explicitly type them, or I can explicitly type them and basically say okay, childrens React note. And the thing that's happening here is if you look at this component, the component uses children, and you're basically expected to put in the children. If you do this, everything will be fine. But if you do this here, you will get a typescript error. So it will tell you like, hey, this component really only works well if you pass in children. So please pass in your children. And you can also do it the other way around. If you leave the children out at all, the beauty is if you do this, it will be fine. But if you do this here, it will give you a warning, hey, you should provide children. You should not provide children because this component doesn't accept any children. So this is a very neat trick. They really fix the types and it really fits my mental model of basically like I mean, this is Wikipedia, but the aim is to prevent operations expecting a certain kind of value from being used with values which that operation does not make sense.
Comments