Speed vs robustness when coding in Typescript

At the risk of sounding like a cliché I thought I’d share some thoughts on striking a balance between “speed” and “robustness”, working as a Typescript developer.

I’ve always been a firm believer in picking the right tool for the job, whether that is applying the appropriate programming language, framework, library or delivery vehicle (ie. chrome extension, vs SaaS app, vs one time small software).

One reason why is that it allows me to make fewer sacrifices and overall better tradeoffs when deciding between speed and robustness.

And while we’re at it, are speed and robustness even opposites on the same, or closely related, scale(s)?

I’m not so sure.

One particular library that comes to mind is Typegoose, a library that delivers a Typescript-fluent implementation of the popular MongoDB adapter for Javascript, Mongoose.

In essence, I feel like this library encapsulates the essence of what I want to address in this post. I don’t think that speed and robustness have to be opposites. In fact, I think they often end up seeming like they are, because we constantly make poor choices.

I don’t think there is any inherent flaw in computers, or how software development is done, that makes it so going faster makes more mistakes. Of course there are mantras that lean heavily into this, such as the Facebook-popularized mantra “move fast and break things”.

Now, I’m not saying there’s no truth to it, because sure there is some truth to the fact that if you want to be more thorough, you have to spend more time.

But that’s also about the extend of it for me.

Because another possible route is to have better processes, and better tools. Today, we’re focusing on the tools, and Typegoose is one such tool.

What does Typegoose allow me to do?

In essence, Typegoose has been a godsent for me, in a time before Mongoose had yet to develop their own Typescript support. That has been addressed since, but I haven’t had the chance to try it.

Being able to move a lot faster, by having my IDE help me identify issues and throw errors when things weren’t as “expected”, is a huge boon to anyones productivity, and thats exactly what it does.

Moving from the relatively complex world of mongoose schemas, to something elegant like this:

@index({ organisationId: 1, providerId: 1 }, { unique: true, sparse: true })
@modelOptions({ schemaOptions: { collection: "users", timestamps: true } })
export default class User extends DocumentCT {
	@prop({ type: String, unique: true, sparse: true, lowercase: true })
	email?: string;

	@prop({ type: String, lowercase: true })
	username?: string;

	@IsBoolean()
	@prop({ type: Boolean })
	active?: boolean;

	@IsDefined()
	@prop({ id: true, index: true, unique: true, required: true })
	userId!: string;

	@prop({ _id: false })
	invitationDetails?: InvitationDetails;
}

It may not seem elegant in this silly editor, but reading over it in my IDE it just feels like Typescript. And that in itself is a win – not having to context switch in order to elegantly be able to express the borders of how a certain model looks, is amazing.

I no longer have to define long, complex and nested definitions if I have complex object structures, I simply define a new class, such as this InvitationDetails:

class InvitationDetails {
	@IsDefined()
	@prop()
	type!: CreateType;

	@prop()
	invitedBy?: string;
}

And as seen from the first example, I can simply reference it and we now have an object relation between the two.

After migrating from Mongoose in a pre-typescript era of it (this must have been back in 2020/2021), being able to read and type entities that seem and feel like Typescript, is a huge productivity boon.

This to me, was certainly a case of “Move fast…. But without breaking anything”, because I enabled better tools for myself.

What are your favorite tools for moving fast, without breaking things?