Use of Mapping Types in TypeScript

I’ve been reading up on my #Typescript and one feature that is under used is mapping types and using them to create new types. 

On some codebases I’ve worked on I’ve seen, and have been guilty of myself, creating new type definitions that maybe extend another Type or are extremely close. For example, a User type and a Staff type both have similar properties (firstname, lastname, email, id etc)

type User = { id,firstname, lastname, email};
type Staff = {id, firstname, lastname, email, jobtitle);

In the need to get a new feature completed it can be quiet easy to create a new Type definition without checking to see if there is something that is already within the code that can be used or extended. Creating new types as we go can lead to cases like above where two types have been created, the original User type and the quickly written Staff type that is needed in a function of a component I’m developing as part of that feature that needs to be done, and I know that I will go back and refactor the code to use an extended or mapped type when I have time (which we all know, we never do).

This is where I feel in my own code mapping types should be used. With a mapping I can create my new type, and if for some reason (API changes or another developer changes the original definition) the remapping of my new type is updated if the original type is changed. So in the case of our User and Staff types if there is a change to the API and title is added as a property which needs to also be displayed in a screen that is making use of the Staff type. By using a Mapped type, this new property is automatically available.

I feel this makes the code more robust and flexible to change, the issue with mapping types is that the syntax is not as clear as using the extends keyword, but really all we are doing in a mapped type is looping over another key and picking what we want from it to create a new type and if that original type changes our derived mapped type can be amended too without to much work.

I think the main aim for how I should write my TypeScript code is to think what are the main data structures in an app, maybe defined by what an API is returning me or what I’m asking for if using GraphQL, and how can set up all the other Types in my application to be created or inferred from these base Types. So my code is both type safe but also flexible enough to handle changes over time. (I think this is the main message Matt Pocock has been trying to get across in his courses and book).

The Benefits of Test-Driven Development

Test-driven development (TDD) is a software development process that relies on writing automated tests before new code is written. The tests are used to ensure that the code meets the requirements of the application.

TDD has a number of benefits, including:

  • Improved quality: TDD can help to improve the quality of software by ensuring that all code is covered by tests. This can help to identify defects early in the development process before they become difficult to fix.
  • Reduced costs: TDD can help to reduce costs by making it easier to refactor code. Refactoring is the process of changing the structure of code without changing its behaviour. With TDD, developers can refactor code with confidence, knowing that the tests will ensure that the changes do not break the application.
  • Increased productivity: TDD can help to increase productivity by making it easier to write code. When developers have a clear understanding of the requirements of an application, they can write code that meets those requirements more quickly.

How to Get Started with TDD

If you are interested in getting started with TDD, there are a few things you can do:

  1. Learn the basics of TDD: There are a number of resources available online and in books that can teach you the basics of TDD.
  2. Find a TDD tool: There are a number of TDD tools available, such as JUnit and NUnit. These tools can help you to write and run tests.
  3. Start with a small project: It is a good idea to start with a small project when you are first getting started with TDD. This will allow you to learn the basics of TDD without feeling overwhelmed.
  4. Get feedback from others: Once you have started using TDD, it is a good idea to get feedback from others. This could include your team members, your manager, or even other developers who are using TDD.

Conclusion

TDD is a powerful tool that can help you to improve quality, reduce costs, and increase the productivity of your software development projects. If you are interested in getting started with TDD, there are a number of resources available to help you.

Contractor Chronicles 3

This week was spent working on the maintenance of an existing Angular application, which has been recently updated to Angular 15.

The upgrade was fairly easy, Angular and the Angular CLI make upgrading between versions so easy. It was something I missed when working with Vue. I know that the Vue CLI does have a way of upgrading, but with the Angular CLI being a bit more mature I find the upgrade process far more straightforward.

I also spent a bit of time watching a live Twitch stream of Josh Goldberg (he’s a full-time open-source developer, who specialises in Typescript), in this stream he was reviewing a pull request to a Typescript project Typescript-ESLint, which is a library for adding linting for Typescript to ESLint.

Linting is a way of checking for bad practices in your code, it is a set of rules for JavaScript. This Typescript-ESLint project adds rules for Typescript to ESLint.

As I mentioned in earlier Contractor Chronicles I said I was looking for an open-source Typescript project to get involved in and I started to look more and more into this Typescript ESLint project and get involved in the project.

So this week I spent a bit more time looking at this project and after watching the Twitch Stream of Josh’s I’m starting to understand how the project works and hopefully soon I can start contributing to the project.

Finally, I spent some time going through a plan for the rest of the year, breaking down some goals I have for the years into projects. Then using the PARA method to organise these projects and goals.

alphabets close up computer connection
Why Use TypeScript?

TypeScript is a free and open-source programming language that was developed by Microsoft. It is a superset of JavaScript that enables developers to write cleaner and more maintainable code. In this blog post, we will discuss why it is beneficial to use TypeScript in your next project.

Improved Code Quality

One of the main advantages of TypeScript is that it provides a type system that enables you to catch errors and improve your code quality. By defining types for your variables, functions, and classes, you can catch errors during development time rather than runtime. This helps you to avoid common bugs, such as null or undefined errors and improves the overall reliability of your code.

Better Tooling

Another benefit of TypeScript is that it provides better tooling support than JavaScript. TypeScript has a rich set of features that enable you to write code faster and with fewer errors. For example, TypeScript provides code completion, which helps you to write code faster and with fewer errors. It also provides better error messages, which helps you to debug your code more easily.

Improved Maintainability

TypeScript makes it easier to maintain code over time. By providing a type system and better tooling support, TypeScript enables developers to write code that is easier to maintain and refactor. This is especially important when working on large projects with many developers. TypeScript’s type system enables developers to understand the codebase better and helps them to make changes more confidently.

In conclusion, TypeScript is a powerful programming language that provides many benefits over JavaScript. It offers improved code quality, better tooling support, and improved maintainability. If you are starting a new project or considering a migration, TypeScript is a great choice that will pay dividends in the long run.

books on shelves
The Anatomy of a Great Typescript Book

If I was to write a TypeScript book, I would aim it at web developers who have started using TS, but come from a JavaScript background and have now been put on a TS-based project.

I wouldn’t aim at a certain web framework like React, or Angular, but how a web developer can start to use all the more advanced features in TypeScript so they can write good, type-safe code for their application, no matter the framework they are using.

In this ‘book,’ I will cover briefly what TypeScript is and its aim. I think it is clear now what TypeScript is and what its original aim was, but if someone is coming to this ‘book’ without knowing anything about TS then a brief introduction is needed. Try to explain to someone who’d be asked or just started using TypeScript why it’s so good and why they should be using it over JavaScript.

I’d then move on to introduce Types, and the primitive types that you have in TS (string, number, boolean) then move on to how to define your Types and the various ways you can do this. How you can create Types from other Types, how to use the utility Types and build up the Types for your project. All the time I try to keep it framework agnostic but relate it to a web developer who is now working on a TS-based project for the first time. So they can see that there are primitive Types, but they aren’t all you have (not everything needs to be a string). How you can start to build your Types that are more descriptive and applicable to your application.

Then I’d start looking at classes and modules and how they can be used along with Types to create a common library that you can use within the components of the framework you are using whether it’s Angular, Vue or React. Building this library of domain-specific Types that describe the data of your application and how you can use classes and modules to structure this library.

After introducing this concept of building a library for your application, which is more domain-specific and provides the business logic your UI components need. I’d move on to using Generics and how they can improve the re-usability of this business logic library.

I found Generics in TypeScript a complex topic to understand, especially if you’ve come from something like JavaScript. They are a powerful concept to understand and to start making use of, when you do it takes the re-usability of your code to another level. I feel that as a web developer who has used a couple of UI frameworks, concepts like Generics and Type manipulation are features I know about but haven’t used much in my applications because so much is handled by the framework. If I learnt more about these advanced concepts of TypeScript they’d improve my code so much.

I would then start to finish the book by looking at the compiler, how it works, and how to make use of the TSConfig settings and compiler. Again through using UI frameworks a lot of the TS compiler settings are set up as part of the framework CLI tooling. Knowing more about this area can lead to more efficient code.

After all that I’d spend a little time looking at the various TypeScript libraries that are available which can be used along with the framework to improve Type safety and maintainability within an application. Libraries like ZOD and tRPC, for example.

Finally, I would then take this concept of learning to build an internal library of business logic for an application and go through how external libraries can be built that can be used to share business logic across multiple applications. If you are a web developer who works for an organisation who have more than an internal application that uses TypeScript and how using features like classes, modules and Generics a Type safe domain-based library can be built and used across all these applications.

I think the aim of this ‘book’ would be to show that, yes TypeScript is used by all the main web frameworks, but there is more to it than just writing the code behind your framework’s components. We learn a lot about frameworks but not so much about the language behind it

Free close up hand typing
Thinking in TypeScript

Throughout all my time with Angular and Vue, the one constant through both frameworks is TypeScript.

Generally, I always thought I knew TypeScript, but over the last couple of months, it’s clear that there is far more to TypeScript and what you can do with TypeScript than I knew.
After watching more and more videos on TypeScript I see that there is so much more I need and want to know about TS. So I’m starting to really study TS, to get to know it and make sure I’m using the advanced features of TS.

My plan for learning advanced TypeScript is:

  • Watch all the Matt Pocock Advanced TypeScript videos
  • Read through Programming TypeScript by Boris Cherry
  • Read Effective TypeScript by Dan Vanderkam
  • Read TypeScript 4 Design Patterns and Best Practices by Theo Despooudis

My aim is to learn the more advanced features of TS so when I have a new feature to add (using either Angular or Vue) or a bug to fix I start to think about the more advanced features of TypeScript e.g. Generics, Types, patterns etc, to how I approach this new feature or bug.