First of all, what is the type build? The type build comes from a test that says basically if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. This is just logical inference that implies that someone can identify a non-subject by observing that subject's characteristics or properties. So we can use this test on the programming world. And yeah, basically we can check some properties and later on guess the type of a certain object.
So why duck typing? This concept brings you flexibility and simplicity so you can have loose coupling code and also for short simpler code. And this encourages better code using, I mean, applying reusability, modularity, and maintainability. So yeah, first of all, how you can implement duck typing on TypeScript using type guards. So what is a type guard? It's a function whose return type is a type predicate. A type predicate is a type assertion that checks if an object has a specific property or set of properties. In this case, we are checking if the pet that could be a fish or a bird has like a stream property. So that allows you to create custom logic to determine whether a value is called upon a specific type. In this case, we are creating this as fish. So we are checking if the pet that could be a fish or a bird, it is indeed a fish. This is a formal type narwhee.
So yeah, this enables type safe code, better compile time and error checking for short. So in this little condition on the left, we are making sure in the first part that pet is always a fish. And since pet could be a fish or a bird on the else statement, we can call period properties or period methods.
So yeah, we are going to start creating this, crafting this generic read application. So first of all, we start with the models. The models are just simple templates. An important part here is the type property. So we are making sure that all our components can have this type property, input, x area, could be a checkbox, could be a button. And also we can have many, many more properties like variant or icon for the buttons or for the accordion. So this is just an example of a container component.
So yeah, after having your models, you can create your type cards. So first of all, you are going to have a CMP variable that is our known object. So could be anything. So yeah, that's why we are creating our type cards. For the text input, for the checkbox, for the button, and also for the accordion. So yeah, we are making sure that after applying these functions, we are checking that this CMP variable is of a certain type.
Comments