All Things Typescript Newsletter - Issue #4
Good morning and welcome to another issue of All Things Typescript. I apologize for not being able to publish last week, as I had a lot on my hand and couldn’t find enough time to work on the content for a full issue. This issue will cover the last two weeks of articles and tweets. Hopefully, you will find this issue very illuminating, and feel free to share it with friends and colleagues.
This issue is divided into the following sections:
This Weeks Lesson - This is new and hopefully you will find this section very useful.
Weekly Challenge - This is also new and will pose challenges that will be answered in the next issue, subscribe to stay updated.
Courses, Articles, and Tweets - This is a list of courses, articles, and tweets from other authors and experts in the field
This Weeks Lesson - Utility Types
In the last issue, I promised to start including lessons in my issues. This week, we will start with something that I really find useful in my day-to-day activities.
These are Utility Types, they help use create types from other types by transforming them. We will look at a few Utility Types in this issue that are useful and then you can continue the process of learning by reading them on the Typescript docs.
Partial Utility Type
This utility type marks all the top levels of an object as optional, allowing you to make a new type with optional fields instead. Please note this works only with top levels and doesn’t apply nested fields:
For instance, if we had the following type:
type Person = {
name: string;
age: string;
}
We can make the fields optional using the Partial<T>
as shown below:
type PartialPerson = Partial<Person>
And this will result in a type similar to this:
type Person = {
name?: string;
age?: string;
}
Required<T> Utility Type
This is the opposite of the above utility type in that it makes all top-level fields that are optional, required. If we had the following type of Person, with age being an optional field:
type Person = {
name: string;
age?: string;
}
We can make it required by using the Required<T>
utility Type as shown below:
type NewPerson = Required<Person>
And this will result in a Type similar to the one below, with age being a required field:
type NewPerson = {
name: string;
age: string;
}
Omit<T> Utility Type
Once in a while, I usually need to create a new type from an existing type by removing some of its fields. The Omit<T> utility type enables me to do this. If we continue with our type of Person above, with name and ages:
type Person = {
name: string;
age: string;
}
We can create a new Type from the above with just the name field, by omitting age as shown below:
type NewPerson = Omit<Person, "age">
This will result in a type similar to the following:
type NewPerson = {
name: string;
}
Pick<T> Utility Type
This utility type operates the opposite of the above utility, by picking the fields you want instead of omitting them. If took the Person
type above, we can create a new type by Picking age instead of omitting it.
type Person = {
name: string;
age: string;
}
Then, using the Pick<T>
utility type, we can pick the fields we are interested in, in this case, age.
type NewPerson = Pick<Person, "age">
And this will result in a type similar to the following:
type Person = {
age: string;
}
Challenge
In the above section, we took a pick into the Utility types and how to use them, this week, I have two challenges for you.
Challenge 1:
In this challenge, I want you to go through typescript docs on Utility types and learn more about Utility Types and how to use them. Focus on the Record<Keys, Type>
and the various ways you can use to help you to make object types more type-safe.
Challenge 2:
This is a little bit more involving but we constitute the lesson for next week. How do Utility types work under the hood and how can you create your own custom utility types?
We will answer the above challenges in the next issue, which will ship on Monday. If you have any questions, ideas, or suggestions please feel free to reach out to me on Twitter - @mwycliffe_dev.
That’s it for the lesson and challenge sections, below are a number of courses, articles, and tweets to help you improve your Typescript skills and knowledge.
Courses
How to Use TypeScript – Beginner-Friendly TS Tutorial
Hi everyone! In this article we’ll talk about what Typescript is, why is it cool, and how can you use it. Table of Contents * Intro * About types * Strings * Numbers * Booleans *
Top Articles
Better data validation in TypeScript | Altostra
Data validation tends to get out of hand and become difficult to maintain and understand. In this article, we explore a better way by using our open-source library
How to use type guards in TypeScript
A type guard is a TypeScript technique used to get information about the type of a variable, usually within a conditional block. Type guards are regular functions that return a boolean, taking a type and telling TypeScript if it can be narrowed down to something more specific.
How Do Enums Work in TypeScript?
Enums, also known as Enumerations, are constants defined that can be used anywhere in the code. They are relatively straightforward to understand. Enums can be defined in Typescript using the enum keyword. This is how we can define one:
Using TypeScript to write compile-time-safe enumeration type handlers
Well written types enforce compile-time guarantees in your code that needs no tests to guarantee functional correctness. At the very least, they ensure necessities are not missed out.
How to master advanced TypeScript patterns
by Pierre-Antoine Mills How to master advanced TypeScript patterns Learn how to create types for curry and Ramda
Becoming a Better Developer Through Open Source - DEV Community
Recently, we at Appwrite hosted a Twitter Space on Becoming A Better Developer Through Open Source… Tagged with opensource, beginners, career, programming.