โ† Back to list

๐Ÿš€ WRITE BETTER TYPESCRIPT TOP 4โ€” YOU SHOULD NOW

Have you ever thought to write more scalable TypeScript code?

Javier Ubeda ยท 2026-05-10 17:38 ยท 0 claps ยท 2.7 min read
#clean-code-architecture #typescript #utility-types
Open on Medium โ†—
Wiki topics: ๐ŸŒ ยท Web Development ๐Ÿ›๏ธ ยท Architecture

๐Ÿš€ WRITE BETTER TYPESCRIPT TOP 4โ€” YOU SHOULD NOW

Have you ever thought to write more scalable TypeScript code?

Letโ€™s suppose youโ€™re building a system to handle employees for a company.

Every employee shares the same common data such as name, email, start data, department they belong or leaving date.

Althought, based on their profiles, they also have specific skills.

Those types may be similar to the following:

type EmployeeProfile = {
  id: string;
  firstName: string;
  lastName: string;
  email: string;
};

type EmploymentDetails = {
  department: string;
  hiredAt: Date;
  isActive: boolean;
  salary: number;
};

Now, based on the previous model.

We need to classify them according to their front and back profiles.

๐Ÿ”ฅ TOP 1 โ€” GENERIC TYPES

type Employee<T> = EmployeeProfile & EmploymentDetails & T;

As you could noticed, Iโ€™ve used Employee<T> and T at the end.

This is known as TypeScript generic types.

In a short terms it allows us to reuse the same Employee structure such as EmployeeProfile and EmploymentDetails for each different profile.

Write these types to assign them to each employee with <> brackets.

type FrontendSkills = {
  programmingLanguage: "TypeScript" | "JavaScript";
  framework: "React" | "Angular" | "Vue";
};

type BackendSkills = {
  programmingLanguage: "Go" | "Java" | "Node.js";
  database: "PostgreSQL" | "MongoDB" | "MySQL";
};

type FrontendDeveloper = Employee<FrontendSkills>;
type BackendDeveloper = Employee<BackendSkills>;

๐Ÿ”ฅ TOP 2 โ€” RECORD

Letโ€™s suppose we want to check all the employee keys to make sure they match with their types and that none are missing.

Thatโ€™s done using Record<Keys, Type>


type EmployeeKeys = keyof EmployeeProfile;

As a result to use keyof, weโ€™ll get every EmployeeProfile keys, just like that:

type EmployeeKeys = "id" | "firstName" | "lastName" | "email";

Next, based on our previous EmployeeKeys above, weโ€™ll use Record to check those keys.

First attempt

const EmployeeValidation: Record<EmployeeKeys, string> = {
  id: "132132112-8991",
  firstName: "Javi",
  lastName: "Mentor",
  email: "recordTypeScript@domain.com"
}

In this case, TypeScript wonโ€™t throw any error, as all fields match their type and already existed.

Second attempt

const EmployeeValidation: Record<EmployeeKeys, number[]> = {
  id: "132132112-8991",
  firstName: 1234,
  email: "recordTypeScript@domain.com"
}

What happened then?

Record will tell TypeScript that there are several errors. โŒ

One of them is that EmployeeKeys expects strings instead than each property containing a list of numbers.

After that, lastName is mandatory and is now missing.

Finally, firstName is also wrong, as it expects a string instead than a number.

This is the code snippet generated on the second attempt.

{
  id: number[];
  firstName: number[];
  email: number[ ];
}

๐Ÿ”ฅ TOP 3 โ€” OMIT / PICK

Letโ€™s say you want to avoid sensitive data like employeeโ€™s salary that shouldnโ€™t go to the frontend.

In this way, you would use Omit to remove it.


type PublicEmployee = Omit<BackendDeveloper, "salary">

Now, imagine youโ€™re building an employee directory page for display a team list.

In my case, I wouldnโ€™t need all the employeesโ€™ details.

Honestly, I would rather their ID numbers, name and the department to display their profile.

type EmployeeCard = Pick<Employee<FrontendSkills>, 
   "id" | "firstName" | "lastName" | "department">;

Finally, whether pick React to render this Employee data with a component, I would do the following code.

const EmployeeList = () => 
{
  return (
    <ul>
      {employees.map((employee) => {

        const { id, firstName, lastName, department } = employee;

        return (
          <li key={id}>
            <h3>
              {firstName} {lastName}
            </h3>
            <p>Department: {department}</p>
          </li>
        );
      })}
    </ul>
  );
};

However, as you could imagine this could be done with other frameworks that support TypeScript like Angular.

Thatโ€™s no problem at all.

๐Ÿ”ฅ TOP 4 โ€” EXTENDS

In this top 4, assume that our main system is growing so fast, and we need to create an isolated Employee interface instead.

Thus, every profile will follow the same properties but with different skills for each profile.

Pretty clean! ๐Ÿงน

interface EmployeeBase extends EmployeeProfile, EmploymentDetails {}

interface FrontendDeveloper extends EmployeeBase {
  programmingLanguage: "TypeScript" | "JavaScript";
  framework: "React" | "Angular" | "Vue";
}

interface BackendDeveloper extends EmployeeBase {
  programmingLanguage: "Go" | "Java" | "Node.js";
  database: "PostgreSQL" | "MongoDB" | "MySQL";
}

๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
06a4f74308cd
slug
write-better-typescript-my-top-3-06a4f74308cd
url
https://medium.com/@javierubeda832/write-better-typescript-my-top-3-06a4f74308cd
canonical_url
https://medium.com/@javierubeda832/write-better-typescript-my-top-3-06a4f74308cd
author_url
https://medium.com/@javierubeda832
status
ok
fetched_at
2026-08-08 16:47:33