Member-only story
TypeScript is very troublesome, don’t want to use it!
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 types
and interfaces
.
When I asked them about the difference between type
and 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 type
and interface
can 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;
};