But right now we want to render it as a button element and we want to be able to pass in an additional icon. And here again we can make use of the TypeScript generics and define our component like that. You can see maybe one of the problems when working with TypeScript a lot. The component code, library code starts to look quite complex but we're achieving a very nice usability for the users of our component.
Like right here for example, we define the type variable T-Component. We add this constraint that it has to be a component type. And then we can say that our link component will receive the S-Prop where we can pass in any component and it can receive all props that this passed component wants to receive in the UI. So now I hope that you could see that yes, TypeScript support is good and all frameworks have the basics down of the TypeScript support but TypeScript support is more than only defining the types inside of our logic of our component, inside of our templates, also between on the boundaries between components but also, there are lots and lots of additional use cases where we can leverage TypeScript in our react projects.
I want to say thank you for listening to my talk, I hope that you have some quite exciting questions for me right now. I wish you a lot of fun at the remaining conference and want to say thank you. Thank you so much.
Would you like to grab a seat? We've got a couple of questions and I'm pretty sure that more questions will be rolling in as we go along. So, one of the first ones, and I kind of like this one because I guess it goes against everything that TypeScript is supposed to offer. Is there ever a good excuse for using the any type in your project? Yeah. There absolutely is. So, the only thing that you really never, never, never, ever must do in your projects is letting an any out of a component. So, it's absolutely okay to use an any within one function, within one module, because maybe the types, you're building a lookup object, for example, where you don't yet know all the fields that will be present on this lookup object, then, just slap an any on that, add the fields later, but make sure that the return type of your function is typed correctly. But within a small module, you can just add an any, maybe write a unit test to be absolutely sure, but it's absolutely no problem to use it within a module, but never let it out of the module, because then it will spread like wildfire. Never let any out, definitely.
Next question, when would you use React node over JSX elements as a return type? So, personally, we almost always use React node, because it's a bit more flexible. So, React node is basically everything that is renderable by React, so, not only a JSX element, but also null, undefined, true, false, strings, numbers, and whatever, so, just a bit more flexible. Nice. And, another question, what do you think is the weakest or hardest part of working with React and TypeScript? I think the hardest part is the error messages that types will give you. Especially maybe one of you, you have experience there, when you're working with GraphQL and write a code generator for your GraphQL types, you receive this massive typed object that, yes, they exactly show you what type of field you receive from the backend, but when one field is missing there in your, maybe you pass it to a component, one field is missing, the error message is just, it's so huge and new developers have, or even intermediate developers, really struggle with identifying where really the error is in this typed message, and this can get really quite confusing, and this is something that still needs improvement, as I say. Always room for improvement. And kind of going back to the question earlier about any, which do you prefer to use, any or unknown. If you absolutely have to, obviously you're never gonna let it get out of the component. So, I'd say within one module, element, function, whatever, I'd say it's fine to use any because then you're just free to do whatever you want with that variable. If you have a case where you're expecting some kind of variable from outside, so you're writing your own console log function, maybe, and users can just supply any value they want, I'd use an unknown, because then I can't accidentally use any fields on this data, because I won't know what data is getting passed in. For example, if I write a log function that just says JSON.stringify, then it really doesn't matter.
Comments