Member-only story

TypeScript is very troublesome, don’t want to use it!

Beck Moulton
6 min readJul 7, 2024

--

Preface

Recently, when our department was developing a component library, I noticed that some team members expressed resistance to using TypeScript. They often complained, “TypeScript is too troublesome, we don’t want to use it!” At first, I was confused: is TypeScript really that troublesome? However, when I took the time to review the team’s code, I finally found the problem. In this article, I want to share some of my findings and solutions with you.

Insufficient types to reuse

During the code review process, I found a lot of duplicate type definitions, which significantly reduced the code’s ability to reuse.

After further communication, I learned that many team members did not know how to reuse types in TypeScript. TypeScript allows us to define types using typesand interfaces.

When I asked them about the difference between typeand interface, most people said they didn't know, which is why they don't know how to reuse types effectively.

Type-defined types can be reused through cross-types ( &), while interface-defined types can be reused through inheritance ( extends). It is worth noting that types defined by typeand interfacecan also be reused by each other. Here are some simple examples:

To reuse the type defined by type**:**

type Point = {
x: number;
y: number;
};type Coordinate = Point & {
z: number;
};

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet